【问题标题】:android:layout_weight="33" how does gone->visible visible->gone effect this?android:layout_weight="33"怎么去->可见可见->去影响这个?
【发布时间】:2012-12-22 22:42:36
【问题描述】:

android 布局使用 layout_weight。我的目标是所有组件的 1/3,但有时页脚实际上设置为消失,然后可见。当设置从消失到可见时,重量计算如何工作?我没有看到重量为 1/3 的线性布局中的内容?

【问题讨论】:

  • 其实应该有人投票赞成这个问题。

标签: android android-layout


【解决方案1】:

似乎有必要在更改其权重后在视图上调用 refreshDrawableState() 才能呈现更改:

((LinearLayout.LayoutParams) btnFav.getLayoutParams()).weight = 3f;
btnFav.refreshDrawableState();

【讨论】:

    【解决方案2】:

    确保将高度设置为 0dp,然后将所有视图的权重设置为 1

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="Hello World, MyActivity"
            android:background="#f00"
            />
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="Hello World, MyActivity"
            android:background="#00f"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="Hello World, MyActivity"
            android:background="#0f0"
            />
    </LinearLayout>
    

    当视图设置为消失时,其余视图将填满它们的屏幕比例。即,如果您将第三个视图设置为消失,则前两个视图将占用布局中每个可用空间的 50%

    此外,如果您希望其余视图每个仅占用 1/3 的空间(即 2/3 已使用,1/3 留空),请将您的隐藏视图设置为不可见而不消失

    【讨论】:

      猜你喜欢
      • 2016-06-16
      • 1970-01-01
      • 1970-01-01
      • 2011-10-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-10
      • 2013-08-01
      • 1970-01-01
      相关资源
      最近更新 更多