如何通过java代码并排编辑两个文本框?

时间:2011-10-30 02:17:18

标签: android

问题已解决

现在我要定义一个编辑文本到左边,另一个到右边... 我试过这样做,但没有工作..当我做fill_parent ..它只显示一个编辑文本..在一行,当我做wrap_content时,它并排显示它们两个.. 现在我想要做的是..两个编辑文本框都定义了左右两侧的特定大小和位置..我试过实现没有工作?

2 个答案:

答案 0 :(得分:1)

您需要在布局XML中将布局从垂直更改为水平:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="horizontal"
            ...

您还可以使用gravity设置在布局中定位元素。因此,要将按钮放在中间,您可以使用:

<Button android:layout_width="wrap_content" 
        android:gravity="center"
        ...

更新:在每行中并排放置两个按钮,只需嵌套LinearLayout元素,例如

<LinearLayout android:orientation="vertical" ...>
  <LinearLayout android:orientation="horizontal" ...>
    <Button ... />
    <Button ... />
  </LinearLayout> 
  <LinearLayout android:orientation="horizontal" ...>
    <Button ... />
    <Button ... />
  </LinearLayout>
</LinearLayout>

答案 1 :(得分:0)

只需创建一个新的线性布局,将EditTexts添加到其中,然后将新的线性布局添加到现有布局中。

public void onClick(View v) {
    EditText t = new EditText(PlusbuttonActivity.this);
    EditText a = new EditText(PlusbuttonActivity.this);
    LinearLayout l = new LinearLayout(PlusbuttonActivity.this);

    t.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    a.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    l.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

    l.addView(t);
    l.addView(a);
    addViewToRoot(l);
} 

基本上,您只是复制了在XML中生成布局所需的相同结构。