【问题标题】:Visibility changed programmatically does not match with weights以编程方式更改的可见性与权重不匹配
【发布时间】:2013-08-23 21:38:05
【问题描述】:

当我更改可见性时,布局保持不变并且未按预期调整大小。 这是我的 XML:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<GameView
    android:id="@+id/gameview"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="5"
    android:layout_gravity="top" />

<TextView android:id="@+id/code"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="4"/>
</LinearLayout>

这是我活动中的代码:

lazy val mCodeView: TextView = findViewById(R.id.code).asInstanceOf[TextView]

def changeState() = {
  mCodeView.setVisibility(if(mCodeView.getVisibility() == View.GONE) View.VISIBLE else View.GONE)
}

但是,当我调用 changeState() 时,Codeview 消失了,但 GameView 没有调整大小。为什么以及如何自动调整它的大小?

【问题讨论】:

    标签: android scala layout resize visibility


    【解决方案1】:

    我最终通过添加一个包含 GameView 的 LinearLayout 和另一个包含 TextView 的 LinearLayout 并更改这些 LinearLayouts 的权重来管理它:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <LinearLayout 
            android:id="@+id/layout1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="5">
            <GameView
                android:id="@+id/gameview"
                android:id="@+id/gameview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="top"  />
        </LinearLayout>
        <LinearLayout  android:id="@+id/layout2"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="4">
            <TextView android:id="@+id/code"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
        </LinearLayout>
    
    </LinearLayout>
    

    在我的代码中:

    private lazy val mLayout2 = findViewById(R.id.layout2).asInstanceOf[LinearLayout]
    
    val params = mLayout2.getLayoutParams().asInstanceOf[LinearLayout.LayoutParams]
    params.weight = 4f - params.weight
    mLayout2.getParent().requestLayout()
    

    希望有一天它会帮助遇到同样问题的人。

    【讨论】:

    • getParent().requestLayout 部分解决了我的问题。
    猜你喜欢
    • 2013-03-09
    • 1970-01-01
    • 2011-03-28
    • 2016-10-18
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多