All Buttons Same Size, Center Text(Android)
I have 6 buttons. The XML is as follows for each one. How to go about changing them so that the button开发者_如何学JAVA text is centered and the they are all the same size?
I assumed that setting the size would do the trick?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/Button1"
android.layout_height="100px"
android.layout_width="60px"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="BUTTON1"/>
Use fill_parent for all child height and layout_weight="1", hope it will work
You should use
android:layout_width="fill_parent"
instead of
android:layout_width="wrap_content"
in button attributes. Button width can be changed by android:padding attribute in LinearLayout Example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:paddingLeft="10px"
android:paddingRight="10px"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/Button1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="BUTTON1"/>
If you want to set the text center of the button use the: android:gravity="center_vertical|center_horizontal" attribute.
For the size use the android:layout_height="100px" and android:layout_width="60px" attributes.
This is the full xml layout of the button:
<Button
android:id="@+id/button"
android:layout_height="100px"
android:layout_width="60px"
android:gravity="center_vertical|center_horizontal"
android:text="Your text!"/>
精彩评论