I have tried all kinds of help from other posts (setting widths to 0dp etc.) but nothing seems to work.
I have 2 layouts and the first one still seems to dominate. If I put background colors on the textviews, they are behaving themselves and end correctly. However put a background on the linear layout and it is almost 3/4 of the screen!?
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:id="@+id/rowkRow"
android:background="@color/RowBkgColor" android:gravity="center_vertical"
android:minHeight="60dp">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/rowleftImage"
android:padding="3dip" android:gravity="left" android:layout_weight="0" />
<LinearLayout android:orientation="vertical"
android:layout_weight="0" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/rowtext1"
android:padding="3dip" android:gravity="left" android:typeface="sans"
android:textStyle="bold" android:text="First Text" android:textSize="40dp"
android:textColor="#FF333344" android:visibility="visible"
/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/rowtext3"
android:padding="3dip" android:gravity="center" android:typeface="sans"
android:textStyle="bold" android:text="First Text" android:textSize="10dp"
android:textColor="#FF333344" android:visibility="visible"
android:background="#606060"
/>
</LinearLayout>
<LinearLayout android:orientation="vertical"
android:layout_weight="1" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/rowtext4"
android:padding="3dip" android:gravity="right" android:text="Fourth Text"
android:textSize="10dp" android:textStyle="bold" android:textColor="@color/RowTextColor"
android:visibility="visible" />
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/rowtext2"
android:padding="3dip" android:gravity="left" android:text="Second Text"
android:textSize="17dp" android:textStyle="bold" android:textColor="@color/RowTextColor"
/>
</LinearLayout>
<ImageView android:id="@+id/rowrightImage"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:padding="3dip" android:layout_weight="0" android:gravity="right" />
</TableRow>
Do the following things. U can solve your problem.
- set the
android:layout_width
of the children to "0dp" - set the
android:weightSum
of the parent - set the android:layout_weight of each child proportionally (e.g.
weightSum = "5"
, three children:layout_weight="1", layout_weight="3", layout_weight="1"
)
来源:https://stackoverflow.com/questions/7104039/putting-a-background-on-the-linear-layout-and-takes-almost-3-4-of-the-screen