简体   繁体   English

在 Android Studio 设计中的多个视图之间切换

[英]Switch between multiple views in Android Studio design

I have an activity that presents a list and some icons.我有一个显示列表和一些图标的活动。 My activity design contains a ConstraintLayout at the top level with the list and the icons as children.我的活动设计在顶层包含一个ConstraintLayout ,其中列表和图标作为子项。

Now there can be a situation where we don't have any data.现在可能会出现我们没有任何数据的情况。 In that case I would like the activity to show neither list nor icons but instead show an image and some error text.在这种情况下,我希望活动既不显示列表也不显示图标,而是显示图像和一些错误文本。

How would I go about implementing this in a way that I can still edit the layout in the Android Studio design view ?我将如何 go以我仍然可以在 Android Studio 设计视图中编辑布局的方式实现这一点? In other words, not just add the image and the error text overlapping my normal activity elements and toggle their visibility programmatically.换句话说,不仅仅是添加与我的正常活动元素重叠的图像和错误文本,并以编程方式切换它们的可见性。 Are there layers or something?有层次还是什么的? Or switchable fragments?还是可切换的片段?

Or like this: How can I group view elements in a way that I can show and hide the whole group in the designer?或者像这样:如何以一种可以在设计器中显示和隐藏整个组的方式对视图元素进行分组?

Yes, in android are Fragments ( Android docs ) and You can create two fragments, one for a situation when data is loaded successfully and second if data isn't loaded.是的,在 android 中是FragmentsAndroid 文档),您可以创建两个片段,一个用于成功加载数据的情况,另一个用于未加载数据的情况。 You can start with the first fragment and when You get information about failed loading data You can switch fragments to second with information about fail.您可以从第一个片段开始,当您获得有关加载数据失败的信息时,您可以使用有关失败的信息将片段切换到第二个片段。

But I think You can do this in one ConstraintLayout layout.但我认为你可以在一个ConstraintLayout布局中做到这一点。 In Your main layout add on top image as You would like to display it and set visibility parameter android:visibility="gone" .在您的主布局中添加您想要显示的顶部图像并设置可见性参数android:visibility="gone" Now You see everything and can create UI in design mode.现在您可以看到所有内容,并且可以在设计模式下创建 UI。 And when You get information about failed with loading data just change image parameter to visible .当您获得有关加载数据失败的信息时,只需将 image 参数更改为visible

To do it programmatically:以编程方式执行此操作:

ImageView imageView = findViewById(R.id.imageDataFailed);
imageView.setVisibility(View.VISIBLE);

How it looks with two containers:两个容器的外观:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--    Here is container for success data load. When You failed load data set visibility="gone"-->
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible">

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button Success"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <!--    Here is container for failed data load. If You want to change design just set `visible`-->
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone">

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button Failed"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM