问题
I have a tablelayout with an image and two buttons. I need to put the two buttons in the same row and the same size. Now I could align the butons in the same row but it's shown in diferents size. The background define the image in the butons but not the size. I define the properties of image in the code. The code of tablelayout
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center_horizontal" >
<ImageView
android:id="@+id/imagePreLoad"
android:layout_width="wrap_content"
android:layout_height="250dp"
android:layout_marginTop="84dp"
android:background="@drawable/lunafons"
android:contentDescription="@string/app_name" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:layout_marginTop="50dp"
android:layout_gravity="center_horizontal"
android:background="@color/black" >
<Button
android:id="@+id/btnload"
android:background="@drawable/btnback"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<Button
android:id="@+id/btnload"
android:background="@drawable/btnloadwallpaper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</TableRow>
</TableLayout>
And the code of background butons:
<?xml version="1.0" encoding="UTF-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"
android:drawable="@drawable/btnload1"
/>
<item android:state_enabled="true"
android:state_pressed="true"
android:drawable="@drawable/btnload2"
/>
<item android:state_enabled="true"
android:state_focused="true"
android:drawable="@drawable/btnload1" />
<item android:state_enabled="true"
android:drawable="@drawable/btnload1" />
Thanks
回答1:
<Button
android:id="@+id/btnload"
android:background="@drawable/btnback"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<Button
android:id="@+id/btnload"
android:background="@drawable/btnloadwallpaper"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
because you're making the width math_parent while it should be 0dp because you are use weight.
回答2:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ButtonsTable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/btnload"
android:background="@drawable/btnback"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<Button
android:id="@+id/btnload"
android:background="@drawable/btnloadwallpaper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</TableRow>
</TableLayout>
来源:https://stackoverflow.com/questions/18429748/android-two-buttons-side-by-side-into-table-row