繁体   English   中英

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

[英]Switch between multiple views in Android Studio design

我有一个显示列表和一些图标的活动。 我的活动设计在顶层包含一个ConstraintLayout ,其中列表和图标作为子项。

现在可能会出现我们没有任何数据的情况。 在这种情况下,我希望活动既不显示列表也不显示图标,而是显示图像和一些错误文本。

我将如何 go以我仍然可以在 Android Studio 设计视图中编辑布局的方式实现这一点? 换句话说,不仅仅是添加与我的正常活动元素重叠的图像和错误文本,并以编程方式切换它们的可见性。 有层次还是什么的? 还是可切换的片段?

或者像这样:如何以一种可以在设计器中显示和隐藏整个组的方式对视图元素进行分组?

是的,在 android 中是FragmentsAndroid 文档),您可以创建两个片段,一个用于成功加载数据的情况,另一个用于未加载数据的情况。 您可以从第一个片段开始,当您获得有关加载数据失败的信息时,您可以使用有关失败的信息将片段切换到第二个片段。

但我认为你可以在一个ConstraintLayout布局中做到这一点。 在您的主布局中添加您想要显示的顶部图像并设置可见性参数android:visibility="gone" 现在您可以看到所有内容,并且可以在设计模式下创建 UI。 当您获得有关加载数据失败的信息时,只需将 image 参数更改为visible

以编程方式执行此操作:

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

两个容器的外观:

<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