如何隐藏相对布局及其全部子元素? [英] How to hide a relative layout and all it's child?

查看:44
本文介绍了如何隐藏相对布局及其全部子元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 setVisibility(View.GONE)隐藏 RelativeLayout .但这是行不通的.请检查我的代码:

I am trying to hide a RelativeLayout using setVisibility(View.GONE). But it is not working. Please check my code:

<LinearLayout>

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

        <TextView />
        <TextView />
        <TextView />
        <ImageView />

    </RelativeLayout>
</LinearLayout>

我正在使用以下代码来更改可见性:

And I am using the following code for changing the visibility:

private RelativeLayout tv;
tv = (RelativeLayout) findViewById(R.id.AccelerometerView);
tv.setVisibility(View.GONE);

我的方法正确吗?我知道我们可以像这样单独隐藏 TextView .但是我想在一个命令中共同隐藏所有三个 TextView ImageView .还有其他办法吗?

Is my approach correct? I know we can hide TextViews like this individually. but I want to hide all three TextViews and the ImageView collectively in a single command. Is there any other way?

推荐答案

您可以创建方法来动态更改布局 tv 的可见性.将布局设置为启动时所需的可见性.

You can create methods to alter the visibility of the layout tv dynamically. Set the layout to whatever visibility you require at start up.

我在此处包括检查,因为它是其他一些代码的一部分,如果您想对可见性进行布尔检查,可能会很有用.

I have included checks here, as it was part of some other code and may prove useful if you ever want to include a boolean check on visibility.

在活动的主体中声明您的视图,以便可以通过任何方法对其进行访问.

Declare your view in the body of your activity, so it can be accessed by any methods.

活动:

RelativeLayout tv;

创建时:

tv = (RelativeLayout) findViewById(R.id.AccelerometerView);

使用这些方法来控制可见性.

Use these methods to control the visibility.

不在创建范围内:

public void setLayoutInvisible() {
    if (tv.getVisibility() == View.VISIBLE) {
        tv.setVisibility(View.GONE);
    }
}
public void setLayoutVisible() {
    if (tv.getVisibility() == View.GONE) {
        tv.setVisibility(View.VISIBLE);
    }
}

如果您不希望其他元素被INVISIBLE元素取代,请使用GONE.您可以在大多数情况下使用此原理.

Use GONE if you don't want the other elements displaced by an INVISIBLE element. You can use this principle for most things.

http://developer.android.com/reference/android/view/View.html

这篇关于如何隐藏相对布局及其全部子元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆