【问题标题】:Android layout weight not working as I thoughtAndroid布局权重不像我想的那样工作
【发布时间】:2013-09-24 16:03:28
【问题描述】:

关注What does android:layout_weight mean?

我有这个:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/buttonToastSimple"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="English Toast" />

    <Button
        android:id="@+id/buttonToastFancy"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:text="French Toast" />

</LinearLayout>

链接告诉我 buttonToastFancy 将占据四分之三的大小 (3/(3+1),但反过来说。根据 Eclipse/我的 AVD,buttonToastSimple 占据四分之三的屏幕大小。

我做错了什么?

【问题讨论】:

    标签: android-layout


    【解决方案1】:
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:weightSum="4"
        android:orientation="horizontal" >
    
        <Button
            android:id="@+id/buttonToastSimple"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="English Toast" />
    
        <Button
            android:id="@+id/buttonToastFancy"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:text="French Toast" />
    
    </LinearLayout>
    

    尝试将想要的属性设置为0dp ..

    例如。如果要设置宽度支持的权重,请使用 android:layout_width="0dp"android:layout_weight="3"。另外,不要忘记父级中的android:weightSum="4"

    【讨论】:

    • 这很好,谢谢。我的 'fill_parent' 值发生了什么事,使它反转了一切?
    • 欢迎,如果有效,请您接受并投票赞成答案。因为 fill parent 意味着填充可用空间,所以当你将它设置为 1 并且有更多可用空间时它会占用它,而不是跟随它的重量。
    • 将宽度值设置为 0dp 正是解决我问题的方法。谢谢!
    【解决方案2】:

    您的视图将占用的空间量计算为 尺寸/布局重量因此具有较低布局重量的视图在您的布局中占据更多空间。将来,您可能还需要使用 layout_weightSum。

    他们是further explained here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多