簡體   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