【问题标题】:android:layout_weight for View behaves the opposite from what is expectedandroid:layout_weight for View 的行为与预期相反
【发布时间】:2014-01-19 19:15:27
【问题描述】:

我正在尝试在 LinearLayout 中分配权重。 虽然它适用于 ButtonView-s,但它对简单视图的作用正好相反:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#FF0000" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:background="#00FF00" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:background="#0000FF" />

</LinearLayout>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#FF0000" />

    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:background="#00FF00" />

    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:background="#0000FF" />

</LinearLayout>

这是 ButtonView 的明确实现,使其正常工作。 但是应该在 View 中实现(以及如何)与 LinearLayout 行为一致? 非常感谢。

【问题讨论】:

  • 两者的输出相同,请重新检查。
  • 对我来说也是一样的输出。
  • 是的,确实 - 我的自定义视图是错误的(
  • @user1986565 你想在你的问题中提出和实现什么?

标签: android android-layout android-linearlayout


【解决方案1】:

你可以尝试用“FrameLayout”替换“View”吗?我不认为View应该被用作持有者

【讨论】:

  • 可能是这样。如果我想实现我的自定义视图并扩展视图怎么办?应该在 View 中实现什么以与 LinearLayout 行为一致?我知道它是 onMeasure()。但是怎么做?我已经这样做了,但它不适用于权重。
【解决方案2】:

在对同样的问题感到沮丧之后,我最终使用了没有文字的TextView

我只在Eclipse 的图形布局模式 中看到过这个问题。在我的 Galaxy S2 上,该应用程序按预期运行。

玩弄原始“视图”的layout_weight 确实会产生非常奇怪的效果。

另一个解决方法是将Viewlayout_height"0dp" 更改为"1dp"。我不知道为什么会修复它,但我猜想一些与零相关的深层魔法(即错误,也许是魔法绦虫)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    • 1970-01-01
    • 2017-03-25
    • 2019-07-12
    • 1970-01-01
    相关资源
    最近更新 更多