gpt4 book ai didi

android - 在android中动态调整 fragment 大小

转载 作者:太空狗 更新时间:2023-10-29 16:36:42 32 4
gpt4 key购买 nike

我有一个 fragment 填满了整个屏幕。现在,如果我添加一个新 fragment ,我希望每个 fragment 填满屏幕的一半。我怎样才能做到这一点? enter image description here

我尝试了这种布局并以编程方式添加了 fragment 。但它不起作用。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="tech.test.org.game.Game$PlaceholderFragment">


<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="50">


</RelativeLayout>

<RelativeLayout
android:id="@+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_below="@+id/relativeLayout"
android:layout_weight="50"
>
</RelativeLayout>

</RelativeLayout>

谢谢。

最佳答案

Kat-hat 给出的答案也是正确的,但是加载 UI 需要很大的负载,这可能会影响性能。

    <RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
>

</RelativeLayout>

<RelativeLayout
android:id="@+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:visibility="gone" >

</RelativeLayout>

</LinearLayout>

当您添加第二个 fragment 时,请以编程方式将 layout2 可见性设置为可见。

findViewById(R.id.relativeLayout2).setVisibility(View.VISIBLE);

关于android - 在android中动态调整 fragment 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27013908/

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