擦除屏幕上的所有内容并显示图像

时间:2012-11-14 10:49:13

标签: android android-layout

我有一个包含各种视图的活动(按钮,文本等)。 我想要做的是以编程方式擦除其中的所有内容,然后将一些图片设置为背景。那当然使用相同的活动而不切换到不同的活动。 这可能吗?

谢谢!

4 个答案:

答案 0 :(得分:1)

如果你真的想要擦除所有可以使用的东西:

ViewGroup.removeAllViews();

在布局的根视图上。

但您可能需要考虑使用ViewSwitcher或仅使用

隐藏内容
View.setVisibility(View.GONE)

而不是能够重复使用它。

答案 1 :(得分:0)

您的所有视图都必须位于某个主要布局中(如果没有,请将它们放在一个这样的布局中)。给该布局一个id,然后使用findViewById()获取该布局,并在其上调用removeAllViews()或removeAllViewsInLayout()方法。

答案 2 :(得分:0)

您可以将所有视图放在一个布局中,并将布局的可见性设置为View.GONE,并将图像可见性设置为可见

答案 3 :(得分:0)

无需实际删除视图。请改用ViewSwitcher

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

<ViewSwitcher
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/viewswitcher"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <ImageView
            android:src="@drawable/your_image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    <LinearLayout
            android:layout_height="match_parent"
            android:layout_width="match_parent">

            <!-- your views here -->

    </LinearLayout>

</ViewSwitcher>

然后,在你的代码中:

ViewSwitcher vs = (ViewSwitcher) findViewById(R.id.viewswitcher);
vs.showNext();
vs.showPrevious();
相关问题