调整布局大小

Resizing a Layout

我在这样的容器中有两个视图。

[ContainerView    
[view 1 (RelativeLayout)]    
[view 2 (RelativeLayout)]    
(Relative Layout)]

and id 喜欢在未给出某个变量时隐藏 view1。我似乎找不到

的解决方案
  1. 完全隐藏view1
  2. resive view2 填充整个 ContainerView

现在我知道这个问题已经被问了十亿次了,但所有的解决方案对我来说都行不通...

部分代码如下:

布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eeeeee"
android:id="@+id/mainview">


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="0dp"
    android:layout_marginBottom="50dp"
    android:elevation="4dp"
    android:background="@android:color/white"
    android:layout_alignParentTop="true"
    android:id="@+id/header">

    <TextView
        android:id="@+id/headertext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:text="Large Text"
        android:padding="15dp"
        android:textColor="@android:color/black"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="50dp"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/upperview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/lowerview"
    android:layout_above="@+id/header">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:elevation="4dp"
        android:id="@+id/vanisherView"
        android:layout_centerHorizontal="true"
        android:layout_margin="20dp">

        <TextView
            android:id="@+id/erklareung"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@android:color/black"
            android:background="@android:color/white"
            android:padding="40dp"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textSize="35dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true" />

    </LinearLayout>


</RelativeLayout>


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="470dp"
    android:id="@+id/lowerview"
    android:padding="20dp"
    android:background="@android:color/white"
    android:elevation="4dp"
    android:layout_above="@+id/bottom">
</RelativeLayout>
        <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="20dp"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:layout_marginLeft="0dp"
    android:id="@+id/bottom"
    android:paddingTop="30dp"
    android:layout_alignParentBottom="true">

    <Button
        android:id="@+id/back"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button"
        android:textSize="30dp" />

    <Button
        android:id="@+id/nextButton"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_weight="0.5"
        android:text="Button"
        android:textSize="30dp"
        android:background="#009688"
        android:textColor="#ffffff" />
</LinearLayout>
</RelativeLayout>

到目前为止我尝试的是

  1. upperview.getLayoutParams().height = 0; lowerview.getLayoutParams().height = 900; mainview.invalidate()
  2. 同requestLayout
  3. 为具有静态尺寸的下部布局创建新的 RelativeLayout
  4. upperview.setVisibility(View.INVISIBLE); lowerview.getLayoutParams().height = 900;

干杯

雅各布斯

我不确定您已经尝试过什么来实现这一目标。为了您的参考,我发布了一个用于实现相同输出的示例代码。

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
    android:id="@+id/rlView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Text Field in View 1" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/rlView2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/rlView1">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Text Field in View 2" />
 </RelativeLayout>

</RelativeLayout>

请注意,您必须使用 rlView1.setVisiblity(View.GONE) 才能使 rlView2 与父视图的大小相同。