Android 以编程方式更改的可见性与权重不匹配
更改可见性时,布局保持不变,不会按预期调整大小。 以下是我的XML:Android 以编程方式更改的可见性与权重不匹配,android,scala,layout,resize,visibility,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_p
<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>
但是,当我调用
changeState()
时,代码视图消失,但游戏视图没有调整大小。为什么以及如何自动调整大小?我最终通过添加一个包含GameView的LinearLayout和另一个包含TextView的LinearLayout,并更改这些LinearLayout的权重来实现这一功能:
<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>
希望有一天它能帮助有同样问题的人。getParent().requestLayout部分解决了我的问题。
<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()