Android在同一行上对齐两个按钮

时间:2012-05-26 06:53:46

标签: java android android-layout dynamically-generated

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

2 个答案:

答案 0 :(得分: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将在你第一次创建时为你定义一个),并且在垂直线性布局中只插入多少个水平线性布局(就像你希望的那样,就像我上面写的那样。

答案 1 :(得分: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);  
相关问题