Reputation: 673
I am new in android and had a query. I am creating a layout with table in it. I have linear layout as main layout and 3 child layout of which i have divided them using layout weight. Now my problem is that how can i divide the table layout in my linear layout in 6 equal parts (Its layout_weight is 0.70).
<TableLayout
android:layout_height="0dp"
android:layout_width="300dip"
android:id="@+id/tablelayout01"
android:layout_weight="0.70"
android:layout_gravity="center"
android:gravity="center"
android:weightSum="1">
<TableRow
android:layout_width="300dp"
android:layout_height="0dp"
android:id="@+id/tablerow01"
android:layout_marginTop="15dp"
android:layout_weight="0.10"
android:gravity="center">
<EditText
android:layout_width="300dp"
android:layout_height="wrap_content"
android:background="@drawable/edittext_bg"
android:ems="10"
android:gravity="center"
android:hint="Name"
android:textColorHint="#333333"
android:shadowRadius="2"
android:shadowColor="#000000"
android:shadowDx="2"
android:shadowDy="4">
<requestFocus />
</EditText>
</TableRow>
<TableRow
android:layout_width="300dp"
android:layout_height="0dp"
android:id="@+id/tablerow02"
android:layout_weight="0.10"
android:gravity="center">
<EditText
android:layout_width="300dp"
android:layout_height="wrap_content"
android:background="@drawable/edittext_bg"
android:ems="10"
android:gravity="center_horizontal"
android:hint="Email"
android:inputType="textEmailAddress"
android:textColorHint="#333333">
<requestFocus />
</EditText>
</TableRow>
<TableRow
android:layout_width="300dp"
android:layout_height="0dp"
android:id="@+id/tablerow03"
android:layout_weight="0.10"
android:gravity="center">
<EditText
android:layout_width="300dp"
android:layout_height="wrap_content"
android:background="@drawable/edittext_bg"
android:ems="10"
android:gravity="center_horizontal"
android:hint="Password"
android:inputType="textPassword"
android:textColorHint="#333333">
<requestFocus />
</EditText>
</TableRow>
<TableRow
android:layout_width="300dp"
android:layout_height="0dp"
android:id="@+id/tablerow04"
android:layout_weight="0.10"
android:gravity="center">
<EditText
android:layout_width="300dp"
android:layout_height="wrap_content"
android:background="@drawable/edittext_bg"
android:ems="10"
android:gravity="center_horizontal"
android:hint="Confirm Password"
android:inputType="textEmailAddress"
android:textColorHint="#333333">
<requestFocus />
</EditText>
</TableRow>
<TableRow
android:layout_width="300dp"
android:layout_height="0dp"
android:id="@+id/tablerow05"
android:layout_weight="0.15">
<Button
android:layout_width="300dp"
android:layout_height="wrap_content"
android:background="@drawable/btn_signup"
android:text="Create Account"
android:textColor="#ffffff">
</Button>
</TableRow>
<TableRow
android:layout_width="300dp"
android:layout_height="0dp"
android:id="@+id/tablerow06"
android:layout_weight="0.15"
android:background="@drawable/fb_button" >
<LinearLayout
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:background="@drawable/fb_button"
android:id="@+id/linearlayout02">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/facebook_icon"/>
<Button
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Sign Up with Facebook"
android:id="@+id/btn_fb"
android:background="@drawable/fb_button"
android:layout_gravity="center"
android:textColor="#ffffff"/>
</LinearLayout>
</TableRow>
</TableLayout>
Upvotes: 3
Views: 2930
Reputation: 8925
If it is 6 equal parts then the parent should be a weightSum
of 6 and each child should have a layout_weight
of 1.
The layout_weight
is defined by the view objects direct parent. So you could have a weightSum
of 6 on the LinearLayout
then a layout_weight
of 1 and a weightSum
of 6 on your TableLayout
and each TablRow
would have a layout_weight
of 1.
Edit
There has to be something in your code that is conflicting with the weight, I have changed my code below to show (3) three items in the LinearLayout
and (6) six in the TableLayout
, I have also included a screenshot of exactly what it gives me in the preview and on my device.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="3" >
<TableLayout
android:id="@+id/tablelayout01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:weightSum="6" >
<TableRow
android:id="@+id/tablerow01"
android:layout_width="300dp"
android:layout_height="0dp"
android:layout_marginTop="15dp"
android:layout_weight="1"
android:gravity="center" >
<EditText
android:layout_width="300dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Name" >
<requestFocus />
</EditText>
</TableRow>
<TableRow
android:id="@+id/tablerow02"
android:layout_width="300dp"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center" >
<EditText
android:layout_width="300dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Email" >
</EditText>
</TableRow>
<TableRow
android:id="@+id/tablerow03"
android:layout_width="300dp"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center" >
<EditText
android:layout_width="300dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Password"
android:inputType="textPassword" >
</EditText>
</TableRow>
<TableRow
android:id="@+id/tablerow04"
android:layout_width="300dp"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center" >
<EditText
android:layout_width="300dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Confirm Password" >
</EditText>
</TableRow>
<TableRow
android:id="@+id/tablerow05"
android:layout_width="300dp"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</TableRow>
<TableRow
android:id="@+id/tablerow06"
android:layout_width="300dp"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</TableRow>
</TableLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:text="Item 2" />
<TextView
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:text="Item 3" />
</LinearLayout>
Upvotes: 2