【问题标题】:Android force LinearLayout to resize views after view removalAndroid强制LinearLayout在视图删除后调整视图大小
【发布时间】:2015-08-15 18:36:50
【问题描述】:

我有一个带有 2 个视图的 LinearLayout,视图 A 占用一些垂直空间,视图 B 占用布局的其余部分。

<LinearLayout

        android:id="@+id/ly2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/input"
        android:orientation="vertical" >

        <View
            android:id="@+id/view1"
            android:layout_width="match_parent"
            android:layout_height="50dp" >
        </View>

        <WebView
            android:id="@+id/webview"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
</LinearLayout>

在某些时候我想删除视图 A。如何告诉 LinearLayout 刷新以便视图 B 占据整个空间?

最好的问候。

【问题讨论】:

  • 使用setVisibility(View.GONE);

标签: android android-linearlayout


【解决方案1】:
View v=(View)findViewById(R.id.view1);

在某些时候使用

v.setVisibility(View.GONE);

查看此link 以获得更多帮助

【讨论】:

  • 我明白了。这可行,但为什么只是从布局中删除视图不会改变布局?
  • 在删除view后尝试linearLayout.invalidate();
【解决方案2】:

尝试用此代码替换您的 xml

<RelativeLayout

        android:id="@+id/ly2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         >

        <View
            android:id="@+id/view1"
            android:layout_width="match_parent"
            android:layout_height="50dp" >
        </View>

        <WebView
            android:id="@+id/webview"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </RelativeLayout>

在你使用这个xml的主要活动中放这个

View view=(View)findViewById(R.id.view1);
view.setVisibility(View.GONE);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-29
    相关资源
    最近更新 更多