Issue
Please have a look at the following code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".HomeScreen" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="*"
android:layout_alignParentTop="true" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</TableRow>
</TableLayout>
</RelativeLayout>
This generates the following output.
But this is not what I need. I need these 12 buttons to fill the entire screen, keep equal spaces between each other and to be equal in size as well. I don't need to set a fixed size lie hight=100, width=100, instead, it should adjust the size according to the device screen size. That means, each button could have 8.3% space in screen (100/12 = 8.3).
I tried this by setting the height of each button to 0dp
and adding the android:layout_weight = 0.083
. But this didn't work because no button were displayed after this.
How can I do this?
Solution
Try the following layout.I have changed some weight values and made button height as fill_parent
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".HomeScreen" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="*"
android:weightSum="4" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="Button" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="Button" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" >
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight=".3"
android:text="Button" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight=".3"
android:text="Button" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight=".3"
android:text="Button" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" >
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight=".3"
android:text="Button" />
<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight=".3"
android:text="Button" />
<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight=".3"
android:text="Button" />
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" >
<Button
android:id="@+id/button10"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight=".3"
android:text="Button" />
<Button
android:id="@+id/button11"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight=".3"
android:text="Button" />
<Button
android:id="@+id/button12"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight=".3"
android:text="Button" />
</TableRow>
</TableLayout>
</RelativeLayout>
This is working on my device.Hope this works for u as well!
Answered By - Manishika
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.