【问题标题】:TableLayout not showing after adding a LinearLayout in Android在 Android 中添加 LinearLayout 后 TableLayout 不显示
【发布时间】:2014-08-07 02:53:34
【问题描述】:
在我添加 @+id/secondLayout 之前一切正常。突然,添加后,tablelayout 不再显示。
有什么想法吗?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/firstLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/secondLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<Chronometer
android:id="@+id/chrono"
android:textColor="#4169E1"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chronometer"
/>
</LinearLayout>
<TableLayout
android:id="@+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/chrono"
>
</TableLayout>
</LinearLayout>
提前感谢您的宝贵时间。
【问题讨论】:
标签:
android
android-linearlayout
add
tablelayout
【解决方案1】:
使用LinearLayout 和TableLayout 高度到"wrap_content",因为子LinearLayout 占用了父布局的空间。所以tablelayout 没有显示。
即试试这个布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/firstLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/secondLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Chronometer
android:id="@+id/chrono"
android:textColor="#4169E1"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chronometer"
/>
</LinearLayout>
<TableLayout
android:id="@+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</TableLayout>
</LinearLayout>
【解决方案2】:
android:layout_below="@id/chrono" 在LinearLayout 中无效,因为它在RelativeLayout 中使用。
将您的 TableLayout 赋予权重为 1,并将您的 android:layout_height 更改为 "wrap_content"
尝试以下数据:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/firstLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/secondLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Chronometer
android:id="@+id/chrono"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chronometer"
android:textColor="#4169E1"
android:textSize="20sp" />
</LinearLayout>
<TableLayout
android:id="@+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#123456"
android:layout_weight="1" >
</TableLayout>
</LinearLayout>
【解决方案3】:
尝试将 Chronometer 高度更改为 wrap_content
尝试使用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/firstLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/secondLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Chronometer
android:id="@+id/chrono"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chronometer"
android:textColor="#4169E1"
android:textSize="20sp" />
</LinearLayout>
<TableLayout
android:id="@+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/chrono" >
</TableLayout>
</LinearLayout>