eclipse 如何一次隐藏Android布局中的所有元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17292687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 20:47:11  来源:igfitidea点击:

How to hide all elements in a Layout in Android at once?

androideclipselayouthideelements

提问by BarniPro

I'm developing an Android app in eclipse.

我正在 eclipse 中开发一个 Android 应用程序。

I have an XML which has 2 linear layouts next to each other. The left one has several buttons, which make content visible in the right. However, I would not only like these buttons to make specific content visible, but hide (setVisibility.GONE) all the other stuff in that layout. I have already tried removeAllViews, but this is not suitable for me since it deletes them. So my idea is to hide (set the visibility to gone) every single stuff, then make the ones I wish visible. The reason I ask this is that it takes too much time setting the visibility to gone for everything (24, actually) for all the 12 buttons.

我有一个 XML,它有 2 个彼此相邻的线性布局。左边的有几个按钮,使右边的内容可见。但是,我不仅希望这些按钮使特定内容可见,而且还隐藏 (setVisibility.GONE) 布局中的所有其他内容。我已经尝试过 removeAllViews,但这不适合我,因为它会删除它们。所以我的想法是隐藏(将可见性设置为消失)每一个东西,然后让我希望的那些可见。我问这个问题的原因是将所有 12 个按钮的所有内容(实际上是 24 个)的可见性设置为消失需要花费太多时间。

Thanks in advance

提前致谢

Layout code:

布局代码:

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

    <ScrollView
        android:id="@+id/scrollView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/imageView3"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/imageView4" >

        <LinearLayout
            android:layout_width="197dp"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/lewis" />

            <Button
                android:id="@+id/first"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/first" />

            <Button
                android:id="@+id/second"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/second" />

        </LinearLayout>
    </ScrollView>

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:scaleType="center"
        android:src="@drawable/cpr1" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:scaleType="center"
        android:src="@drawable/epizodok" />

    <LinearLayout
        android:id="@+id/def"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/imageView3"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/scrollView2"
        android:layout_toRightOf="@+id/scrollView2"
        android:orientation="vertical"
        android:visibility="visible" >

        <ImageView
            android:id="@+id/image1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/lewis"
            android:visibility="gone" />

        <ImageView
            android:id="@+id/image2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"
            android:visibility="gone" />

    </LinearLayout>

</RelativeLayout>

回答by Mr.Sandy

Please try this...

请试试这个...

<LinearLayout
        android:id="+id/ll"
        android:layout_width="197dp"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <!-- All content which you hide/show put here -->

</LinearLayout>

now in your class file you read this.

现在在你的类文件中你读到了这个。

LinearLayout ll = (LinearLayout) findViewById(R.id.ll);

after that as per your requirement you hide/show this Layout with their all contents.

之后,根据您的要求,您可以隐藏/显示此布局及其所有内容。

like: For Hide.

喜欢:为了隐藏。

ll.setVisibility(View.GONE);

For Show.

为秀。

ll.setVisibility(View.VISIBLE);

when you hide/show LinearLayout all content with this Layout also hide/show automatically.

当您隐藏/显示 LinearLayout 时,此布局的所有内容也会自动隐藏/显示。

回答by Bhoomika Brahmbhatt

Simply, Just take another Linear Layout with vertical layout within your "def" Linear layout..Nd change its visibility as per your need.

简单地说,只需在“定义”线性布局中采用另一个具有垂直布局的线性布局..Nd 根据您的需要更改其可见性。

<LinearLayout
        android:id="@+id/def"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/imageView3"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/scrollView2"
        android:layout_toRightOf="@+id/scrollView2"
        android:orientation="vertical"
        android:visibility="visible" >
     <LinearLayout
        android:id="@+id/Subdef"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/image1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/lewis"
            android:visibility="gone" />

        <ImageView
            android:id="@+id/image2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"
            android:visibility="gone" />
</LinearLayout>
    </LinearLayout>

回答by Raghu Mudem

No need to hide views instead of that just use ViewFlipper concept. Just declare view flipper in your xml file with your two linearlayouts like this

无需隐藏视图,而只需使用 ViewFlipper 概念。只需在您的 xml 文件中声明视图翻转器,并使用您的两个线性布局,如下所示

    <Viewflipper android:id="@+id/myViewFlipper" android:layout_height="wrap_content" android:layout_width="match_parent">
    <LinearLayout android:id="@+id/layout1" android:layout_height="wrap_content" android:layout_width="match_parent"></LinearLayout>
    <LinearLayout android:id="@+id/layout2" android:layout_height="wrap_content" android:layout_width="match_parent"></LinearLayout>
   </Viewflipper>

And get viewflipper reference in your java code like this

并像这样在您的 Java 代码中获取 viewflipper 引用

Viewflipper mFlipper = (Viewflipper)findViewById(R.id.myViewFlipper);

use mFlipper.showNext(), use mFlipper.showPrevious() methods to hide and show your layouts thats it.

使用 mFlipper.showNext(),使用 mFlipper.showPrevious() 方法来隐藏和显示您的布局。