gpt4 book ai didi

android - 以编程方式设置 LinearLayout 子项的权重

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:08:53 24 4
gpt4 key购买 nike

<分区>

我有一个垂直的 LinearLayout LL_p,它有两个水平的子级 LinearLayouts LL_1 和 LL_2,它们又有自己的子级。基于LL_1和LL_2的可见内容,我想动态改变它们在LL_p中的相对权重。我已经有了一个包含大量细节的 xml 布局,我不想丢失这些细节,所以我只需要对权重进行增量更改。我怎么做?这是我的 xml


<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=“0.2”
android:orientation="vertical" >

<LinearLayout
android:id="@+id/ll_1”
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.55"
android:background="@drawable/some_image”
android:orientation="vertical" >

<!—- a number of includes —>
</LinearLayout>

<LinearLayout
android:id="@+id/ll_2”
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.45"
android:layout_marginBottom="@dimen/dim_1”
android:background="@color/some_color”
android:orientation="horizontal" >

<!—- a number of children —>
</LinearLayout>
</LinearLayout>

</LinearLayout>

所以在 java Activity 类中,我认为我需要以下方法,但我需要帮助来完成它。请注意,我没有实例化新的布局参数,因为那样会导致我丢失 xml 布局细节;相反,我使用 getLayoutParams 从 xml 中获取一组。那么如何对获得的布局进行权重更改?

private void adjustMYLayout(boolean flip) {
LayoutParams layout1 = mLL1.getLayoutParams();
LayoutParams layout2 = mBLL2.getLayoutParams();
//now what?
if(flip) {//set one weight system
}else {
//set other weight system
}
}

更新@nKn

private void adjustMYLayout(boolean flip) {

if (flip) {
mLL1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.8f));
mLL2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.2f));
} else {
mLL1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.55f));
mLL2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT, 0.45f));
}
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com