我有一个标题有两个按钮,一个文本一个按钮左对齐,另一个右对齐。现在我想将这些按钮之间的文字居中。我可以将所有内容垂直居中,但无法在按钮之间水平居中文本。按钮之间的中心文本
目前,它看起来像这样
:B:文字:B:
这就是我想要
:B:文字:B:
我有一个标题有两个按钮,一个文本一个按钮左对齐,另一个右对齐。现在我想将这些按钮之间的文字居中。我可以将所有内容垂直居中,但无法在按钮之间水平居中文本。按钮之间的中心文本
目前,它看起来像这样
:B:文字:B:
这就是我想要
:B:文字:B:
<LinearLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_weight="1.0"
android:text="Some Text" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2" />
</LinearLayout>
不是button2的位置取决于textview的长度吗? – user1163392 2012-03-13 19:54:38
如果textview有一个布局权重 – dldnh 2012-03-13 19:55:10
是的,工作,谢谢 – user1163392 2012-03-13 19:57:22
这样做你在找什么。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Button" android:layout_centerVertical="true"/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/button2"
android:layout_toRightOf="@+id/button1"
android:text="TextView" android:gravity="center"/>
</RelativeLayout>
你能发表相关代码的片段吗? – Shaunak 2012-03-13 19:43:31
我想要的只是按钮1垂直居中并左对齐到父对齐,按钮2垂直对齐并右对齐父对象。然后在这两个按钮之间水平和垂直居中的文本视图。 – user1163392 2012-03-13 19:50:13
使用相对布局http://developer.android.com/resources/tutorials/views/hello-relativelayout.html – 2012-03-13 19:51:40