一个布局文件,而不是许多类似文件

问题描述

我有几个(大约10个活动)XML布局文件,它们看起来都非常相似。在这里您可以看到其中之一:

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

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:id="@+id/container_frame_softrdinks"/>

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar_mainActivity"
        android:layout_width="432dp"
        android:layout_height="135dp"
        android:background="#435cb53f"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:titleTextColor="@android:color/holo_green_light">

        <TextView
            android:id="@+id/textView_ToolBar_CocktailSelectionActivity"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:gravity="center"
            android:layout_gravity="center"
            android:textColor="@android:color/white"
            android:textSize="24sp"
            android:text="Softdrinks" />
    </androidx.appcompat.widget.Toolbar>

    <ScrollView
        app:layout_constraintTop_toBottomOf="@id/toolbar_mainActivity"
        app:layout_constraintBottom_toTopOf="@id/bottom_navigation"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_width="0dp"
        android:layout_height="0dp">
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:context=".MainActivity"
            tools:ignore="ExtraText">


            <ImageButton
                android:id="@+id/imageButton2"
                android:layout_width="0dp"
                android:layout_height="128dp"
                android:layout_marginTop="12dp"
                android:background="#00000000"
                android:scaleType="fitCenter"
                app:layout_constraintEnd_toStartOf="@id/imageButton1"
                app:layout_constraintHorizontal_chainStyle="spread"
                app:layout_constraintHorizontal_weight="1"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/test_dish_1" />

            <ImageButton
                android:id="@+id/imageButton1"
                android:layout_width="0dp"
                android:layout_height="128dp"
                android:layout_marginTop="12dp"
                android:background="#00000000"
                android:scaleType="fitCenter"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_weight="1"
                app:layout_constraintStart_toEndOf="@id/imageButton2"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/test_dish_2" />

            <ImageButton
                android:id="@+id/imageButton3"
                android:layout_width="0dp"
                android:layout_height="128dp"
                android:layout_marginTop="12dp"
                android:background="#00000000"
                android:scaleType="fitCenter"
                app:layout_constraintEnd_toStartOf="@id/imageButton4"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/imageButton2"
                app:layout_constraintVertical_chainStyle="packed"
                app:srcCompat="@drawable/test_dish_2" />

            <ImageButton
                android:id="@+id/imageButton4"
                android:layout_width="0dp"
                android:layout_height="128dp"
                android:layout_marginTop="12dp"
                android:background="#00000000"
                android:scaleType="fitCenter"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@id/imageButton3"
                app:layout_constraintTop_toBottomOf="@id/imageButton2"
                app:srcCompat="@drawable/test_dish_2" />

            <ImageButton
                android:id="@+id/imageButton5"
                android:layout_width="0dp"
                android:layout_height="128dp"
                android:layout_marginTop="12dp"
                android:background="#00000000"
                android:scaleType="fitCenter"
                app:layout_constraintVertical_chainStyle="packed"
                app:layout_constraintEnd_toStartOf="@id/imageButton6"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/imageButton3"
                app:srcCompat="@drawable/test_dish_2" />

            <ImageButton
                android:id="@+id/imageButton6"
                android:layout_width="0dp"
                android:layout_height="128dp"
                android:layout_marginTop="12dp"
                android:background="#00000000"
                android:scaleType="fitCenter"
                app:layout_constraintStart_toEndOf="@id/imageButton5"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="@id/imageButton3"
                app:srcCompat="@drawable/test_dish_1" />


            <ImageButton
                android:id="@+id/imageButton7"
                android:layout_width="0dp"
                android:layout_height="128dp"
                android:layout_marginTop="12dp"
                android:background="#00000000"
                android:scaleType="fitCenter"
                app:layout_constraintVertical_chainStyle="packed"
                app:layout_constraintEnd_toStartOf="@id/imageButton8"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/imageButton5"
                app:srcCompat="@drawable/test_dish_2" />

            <ImageButton
                android:id="@+id/imageButton8"
                android:layout_width="0dp"
                android:layout_height="128dp"
                android:layout_marginTop="12dp"
                android:background="#00000000"
                android:scaleType="fitCenter"
                app:layout_constraintStart_toEndOf="@id/imageButton7"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="@id/imageButton5"
                app:srcCompat="@drawable/test_dish_2" />


            <ImageButton
                android:id="@+id/imageButton9"
                android:layout_width="0dp"
                android:layout_height="128dp"
                android:layout_marginTop="12dp"
                android:background="#00000000"
                android:scaleType="fitCenter"
                app:layout_constraintVertical_chainStyle="packed"
                app:layout_constraintEnd_toStartOf="@id/imageButton10"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/imageButton7"
                app:srcCompat="@drawable/test_dish_2" />

            <ImageButton
                android:id="@+id/imageButton10"
                android:layout_width="0dp"
                android:layout_height="128dp"
                android:layout_marginTop="12dp"
                android:background="#00000000"
                android:scaleType="fitCenter"
                app:layout_constraintStart_toEndOf="@id/imageButton9"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="@id/imageButton7"
                app:srcCompat="@drawable/test_dish_2" />


            <ImageButton
                android:id="@+id/imageButton11"
                android:layout_width="0dp"
                android:layout_height="128dp"
                android:layout_marginTop="12dp"
                android:background="#00000000"
                android:scaleType="fitCenter"
                app:layout_constraintVertical_chainStyle="packed"
                app:layout_constraintEnd_toStartOf="@id/imageButton12"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/imageButton9"
                app:srcCompat="@drawable/test_dish_2" />

            <ImageButton
                android:id="@+id/imageButton12"
                android:layout_width="0dp"
                android:layout_height="128dp"
                android:layout_marginTop="12dp"
                android:background="#00000000"
                android:scaleType="fitCenter"
                app:layout_constraintStart_toEndOf="@id/imageButton11"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="@id/imageButton9"
                app:srcCompat="@drawable/test_dish_2" />


            <ImageButton
                android:id="@+id/imageButton13"
                android:layout_width="0dp"
                android:layout_height="128dp"
                android:layout_marginTop="12dp"
                android:background="#00000000"
                android:scaleType="fitCenter"
                app:layout_constraintVertical_chainStyle="packed"
                app:layout_constraintEnd_toStartOf="@id/imageButton14"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/imageButton11"
                app:srcCompat="@drawable/test_dish_1" />

            <ImageButton
                android:id="@+id/imageButton14"
                android:layout_width="0dp"
                android:layout_height="128dp"
                android:layout_marginTop="12dp"
                android:background="#00000000"
                android:scaleType="fitCenter"
                app:layout_constraintStart_toEndOf="@id/imageButton13"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="@id/imageButton11"
                app:srcCompat="@drawable/test_dish_2" />



        </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>




    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        app:labelVisibilityMode="labeled"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorGreen"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_navigation"
        app:itemIconTint="@color/colorPrimaryDark"
        app:itemTextColor="@color/colorAccent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

现在的问题是,对于所有不同的活动,是否仅可能有一个XML布局文件(建议)。我个人看到以下问题:

  1. 每个布局文件中的底数均不相同。由于我在ConstraintLayout内部使用了ScrollView,所以我不知道如何识别布局。我必须在XML布局文件中为所有ImageButtoms手动设置约束
  2. 我没有在每个XML布局文件中使用普通的Buttoms,而是ImageButtons。创建XML布局文件时,我必须指定要在ImageButtons上显示图片。我不知道如何动态地做到这一点

因此,我对每一个评论都很满意,非常感谢您的帮助。

没有人知道这是否可行以及如何做这样的事情吗?

解决方法

您可以将Stuff添加到Layout中,并在运行时完全管理布局的参数(通常在OnCreate或OnCreateView中完成(取决于是Activity还是Fragment) 只需使用AppBar等进行基本布局,然后按代码添加图像按钮即可。