Gar*_*ner 7 android android-fragments android-layout-weight
我有三个片段,前两个填充80%的屏幕,最后一个填充其余的片段(这个片段永远不会改变大小).我希望,在用户(焦点)输入片段后,调整片段的大小,使其填满屏幕的70%(将10%留给另一个).像这样:

可以通过动态改变碎片的重量吗?或者有更好的方法来实现这一目标?
这是我现在在XML中的代码:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1.0">
<FrameLayout android:id="@+id/containerParent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".8">
<LinearLayout
android:id="@+id/MainLinear"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="1.0">
<FrameLayout android:id="@+id/fragment1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight=".5"/>
<FrameLayout android:id="@+id/fragment2"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight=".5"/>
</LinearLayout>
</FrameLayout>
<FrameLayout android:id="@+id/fragment3"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight=".2"/>
</LinearLayout>
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)
它应该是这样的:
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, WEIGTH_HERE);
Run Code Online (Sandbox Code Playgroud)
假设 WEIGHT_HERE 是一个浮点数(可能需要强制转换)。
(我不知道这是否是一个好方法,但它应该做你想做的事)
如果您想将其与按钮或其他东西一起使用,只需在不使用 WEIGHT 参数的情况下执行相同的操作并添加:
Button b = new Button(this);
param.weight= (float) 0.5 // 0.5f
b.setLayoutParams(param);
Run Code Online (Sandbox Code Playgroud)
但此方法将创建一个新的布局参数,因此如果您想保留所有内容,只需执行相同操作但不创建新布局,请通过获取它来编辑现有布局:
LinearLayout.LayoutParams mLay =
(LinearLayout.LayoutParams)myLay.getLayoutParams();
mLay.weight = (float) WEIGTH // WEIGHTf
Run Code Online (Sandbox Code Playgroud)