1

我需要对齐button,textview 在同一行,按钮对齐右侧,textview也对齐右侧
我尝试了很多方法,但首先对齐textview然后对齐按钮,如何解决这个问题请任何人帮助并以编程方式解决我的问题,在布局 xml 设计中取得了一致的成功,但我以编程方式需要它。

4

2 回答 2

2

将两个视图放在布局中并将方向设置为“水平”。

<LinearLayout>

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

线性布局也可以相互嵌套!

如果你想实现更多的视图行,那么你可以定义一个垂直布局(main.xml 在第一次创建时默认为你定义一个),然后在该垂直线性布局中插入多少水平线性布局(如我在上面写)如你所愿。

于 2012-05-26T08:31:11.210 回答
1

像这样的东西应该工作。您应该修改它以满足您的需要(即设置适当的文本大小、宽度、高度等)。

TextView tv = new TextView(this);  
tv.setText("Text");  

Button bt = new Button(this);  
bt.setText("Button");  

LinearLayout ll = new LinearLayout(this);  
ll.setOrientation(LinearLayout.HORIZONTAL);  
ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));  
ll.addView(tv);  
ll.addView(bt);
setContentView(ll);  
于 2012-05-26T07:06:18.133 回答