TableLayout, android:layout_weight, and include



我有一个TableLayout中包含6个布局的布局。我想在横向模式下这样做,因为ScrollView在我的场景中会很尴尬。

我有一个48dp高的水平布局,它包含一个正方形的ImageView、一个全重的SeekBar和一个正方形ImageButton。我用Java设置了ImageView的可绘制项。

在主布局中,我为每个列使用一个include,并试图使列均匀,因为它们只占屏幕的一半,顺便说一句,这是一个以Theme.Holo.Dialog为主题的活动。

然而,当我运行该应用程序时,我会得到40-60%的不平衡视图。为什么?我做错了什么?

布局land/main.xml的一部分

<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
      ... >
...
<TableLayout
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:stretchColumns="1"
    android:layout_height="wrap_content">
    <TableRow android:layout_width="match_parent" android:layout_height="wrap_content">
        <include android:layout_weight="1" layout="@layout/toast_item" android:id="@+id/systemToast"/>
        <include android:layout_weight="1" layout="@layout/toast_item" android:id="@+id/ringerToast"/>
    </TableRow>
    <TableRow android:layout_width="match_parent" android:layout_height="wrap_content">
        <include android:layout_weight="1" layout="@layout/toast_item" android:id="@+id/notifyToast"/>
        <include android:layout_weight="1" layout="@layout/toast_item" android:id="@+id/mediaToast"/>
    </TableRow>
    <TableRow android:layout_width="match_parent" android:layout_height="wrap_content">
        <include android:layout_weight="1" layout="@layout/toast_item" android:id="@+id/alarmToast"/>
        <include android:layout_weight="1" layout="@layout/toast_item" android:id="@+id/inCallToast"/>
    </TableRow>
</TableLayout>
...
</LinearLayout>

布局land/toat_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="0dp"
   android:layout_height="48dp"
   android:layout_marginLeft="8dp"
   android:layout_marginRight="8dp"
   android:orientation="horizontal"
   android:gravity="center_vertical">
<ImageView
    android:id="@+id/icon"
    android:src="@drawable/ic_audio_alarm"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:scaleType="center"
    android:clickable="false"
    android:layout_weight="0"/>
<SeekBar
    android:layout_width="0dp"
    android:layout_height="48dp"
    android:layout_weight="1"
    android:gravity="center_vertical"
    android:layout_marginLeft="4dp"
    android:maxHeight="1000dp"
    android:layout_marginRight="4dp"
    android:id="@+id/seekbar"/>
<ImageButton
    android:id="@+id/schedules"
    android:layout_width="48dp"
    android:layout_height="match_parent"
    android:scaleType="center"
    android:layout_weight="0"
    android:src="@drawable/ic_action_event"
    android:background="?android:attr/selectableItemBackground"
/>
</LinearLayout>

我注意到你有android:layout_weight,但我似乎没有android:weightSum

我建议您使用android:weightSum="2",这样您的两个具有android:layout_weight="1"的对象将均匀分布。

根据Android文档:

如果有三个文本字段,其中两个声明权重为1,而另一个没有权重,第三个文本字段没有重量就不会生长,只会占据其内容。另外两个将相等地扩展以填充空间在测量完所有三个场之后剩余。

如果第三个字段为然后给它一个2(而不是0)的权重,那么它现在被声明为更多比其他两个都重要,所以它只占剩余总数的一半空间,而前两者平分其余部分。

最新更新