【问题标题】:Dynamically resize fragment in android在android中动态调整片段大小
【发布时间】:2014-11-19 10:01:32
【问题描述】:

我有一个充满整个屏幕的片段。现在,如果我添加一个新片段,我希望每个片段都占据屏幕的一半。我该怎么做?

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

<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>

谢谢。

【问题讨论】:

    标签: android android-layout android-fragments android-xml


    【解决方案1】:

    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>
    

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

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

    【讨论】:

    • 但是如果你这样做,那么片段 A 的一半会被隐藏并且不会调整大小,对吧?
    【解决方案2】:

    使用线性布局而不是相对布局并将布局设置为

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:weightSum="100" >
    
        <LinearLayout
            android:id="@+id/layout1"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="100" >
    
            <fragment
                android:id="@+id/fragment1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                >
            </fragment>
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/layout2"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="50"
            android:visibility="gone" >
    
            <fragment
                android:id="@+id/fragment2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                >
            </fragment>
        </LinearLayout>
    
    </LinearLayout>
    

    当您添加第二个片段时,请以编程方式将 layout2 可见性设置为可见,并将 layout1 权重设置为 50

    LinearLayout layout1 = (LinearLayout) findViewById(R.id.layout1);
    android.widget.LinearLayout.LayoutParams par = layout1.getLayoutParam();
    par.weight =50;
    

    【讨论】:

      猜你喜欢
      • 2017-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-20
      • 2011-08-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多