我有三个 fragment ,前两个占据屏幕的 80%,最后一个占据其余部分(这个尺寸永远不会改变)。我想在 fragment 中的用户(焦点)输入后调整 fragment 大小,使其填充屏幕的 70%(将 10% 留给另一个)。像这样:
可以动态改变 fragment 的权重吗? 或者有更好的方法来实现这一点?
这是我现在在 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>
最佳答案
它应该是这样的:
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, WEIGTH_HERE);
假设 WEIGHT_HERE 是一个 float (可能需要转换)。
(我不知道这样做是不是一个好方法,但它应该做你想做的事)
如果你想将它与按钮或其他东西一起使用,只需在没有按钮的情况下执行相同的操作 WEIGHT 参数并添加:
Button b = new Button(this);
param.weight= (float) 0.5 // 0.5f
b.setLayoutParams(param);
但是这个方法会创建一个新的布局参数,所以如果你想保留所有的东西只是做同样的事情而不是创建一个新的布局,通过获取它来编辑现有的布局:
LinearLayout.LayoutParams mLay =
(LinearLayout.LayoutParams)myLay.getLayoutParams();
mLay.weight = (float) WEIGTH // WEIGHTf
关于android - 如何通过焦点调整屏幕中两个 fragment 的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29891497/