以编程方式更改的可见性与权重不匹配

时间:2013-08-21 13:26:59

标签: android scala layout resize visibility

当我更改可见性时,布局保持不变,并且未按预期调整大小。 这是我的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不会调整大小。为什么以及如何自动调整大小?

1 个答案:

答案 0 :(得分:3)

我最终通过添加一个封闭GameView的LinearLayout和另一个封闭TextView的函数并更改这些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()

希望有一天能帮助有同样问题的人。