【问题标题】:layout_weight working in simulator, not on devicelayout_weight 在模拟器中工作,而不是在设备上
【发布时间】:2016-10-03 05:59:51
【问题描述】:

我有一个线性布局,其中包含两个列表视图、一个文本视图和另一个用于容纳一些按钮的线性布局。我希望第二个列表视图的高度是第一个的两倍。我已将两个列表视图的高度设置为 0dp,并将第一个 layout_weight 设置为 1,第二个设置为 2,然后将包含视图的 weightSum 设置为 3。这是实际布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:weightSum="3"
    android:layout_height="match_parent">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/categoryList" />
    <ListView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:id="@+id/itemList" />
    <TextView
        android:id="@+id/walletStr"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/cancelBtn"
            android:text="cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/buyBtn"
            android:text="buy"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>

在模拟器上,这会产生预期的效果,但在实际设备上,几乎所有空间都到了顶部列表视图。

有什么想法吗?提前致谢。

【问题讨论】:

  • 去掉weightSum外层LinearLayout的属性。

标签: android android-linearlayout


【解决方案1】:

线性布局不仅有 2 个列表,还有更多组件,您必须考虑。 weightSum 应该除以这个线性布局的所有组件。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:weightSum="7"
    android:layout_height="match_parent">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:id="@+id/categoryList" />
    <ListView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="4"
        android:id="@+id/itemList" />
    <TextView
        android:id="@+id/walletStr"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"/>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.5">
        <Button
            android:id="@+id/cancelBtn"
            android:text="cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/buyBtn"
            android:text="buy"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    • 1970-01-01
    • 2012-08-12
    • 1970-01-01
    • 2011-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多