Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何为以下布局创建自定义视图?_Java_Android_Android Studio_Android Layout - Fatal编程技术网

Java 如何为以下布局创建自定义视图?

Java 如何为以下布局创建自定义视图?,java,android,android-studio,android-layout,Java,Android,Android Studio,Android Layout,我的home_activity.xml中有几个tile,我想为tile创建一个自定义视图,这样我就不必为每个tile单独编写每个属性 下面是一段代码片段,用于在我要为其创建自定义视图的主活动中的每个磁贴中添加组件。我已经包括了两个这样的瓷砖的例子 <LinearLayout android:layout_width="0dp" android:layout_height="wra

我的home_activity.xml中有几个tile,我想为tile创建一个自定义视图,这样我就不必为每个tile单独编写每个属性

下面是一段代码片段,用于在我要为其创建自定义视图的主活动中的每个磁贴中添加组件。我已经包括了两个这样的瓷砖的例子

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="12dp"
                    android:clipToPadding="false"
                    android:gravity="center"
                    android:orientation="vertical"
                    app:layout_column="0"
                    android:paddingBottom="@dimen/home_card_elev"
                    app:layout_columnWeight="1"
                    app:layout_gravity="fill_horizontal"
                    app:layout_row="0">

                    <androidx.constraintlayout.widget.ConstraintLayout
                        android:id="@+id/contact_tracer_tile"
                        android:layout_width="150dp"
                        android:layout_height="match_parent"
                        android:background="@drawable/buttonback2"
                        android:clickable="true"
                        android:elevation="@dimen/home_card_elev"
                        app:layout_column="0"
                        app:layout_row="1">

                        <ImageButton
                            android:id="@+id/image_button3"
                            style="@android:style/Widget.Material.Light.ImageButton"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/imgbut_text3"
                            android:layout_alignParentStart="true"
                            android:layout_alignParentEnd="true"
                            android:layout_alignParentBottom="true"
                            android:layout_marginBottom="2dp"
                            android:background="#FFFFFF"
                            android:clickable="false"
                            android:contentDescription="Card Image"
                            app:layout_constraintBottom_toBottomOf="parent"
                            app:layout_constraintEnd_toEndOf="parent"
                            app:layout_constraintStart_toStartOf="parent"
                            app:layout_constraintTop_toBottomOf="@+id/imgbut_text3"
                            app:layout_constraintVertical_bias="0.0"
                            app:srcCompat="@drawable/ic_symptom_tracker" />

                        <TextView
                            android:id="@+id/imgbut_text3"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="4dp"
                            android:fontFamily="@font/mukta_semibold"
                            android:text="@string/social_distancing_tile"
                            android:textAlignment="center"
                            android:textColor="@color/blackcolor"
                            android:textSize="@dimen/home_card_font"
                            app:layout_constraintTop_toTopOf="parent" />

                    </androidx.constraintlayout.widget.ConstraintLayout>

                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="12dp"
                    android:clipToPadding="false"
                    android:gravity="center"
                    android:paddingBottom="@dimen/home_card_elev"
                    android:orientation="vertical"
                    app:layout_column="1"
                    app:layout_columnWeight="1"
                    app:layout_gravity="fill_horizontal"
                    app:layout_row="0">

                    <androidx.constraintlayout.widget.ConstraintLayout
                        android:id="@+id/onair_tile"
                        android:layout_width="150dp"
                        android:layout_height="match_parent"
                        android:background="@drawable/buttonback2"
                        android:clickable="true"
                        android:elevation="@dimen/home_card_elev"
                        app:layout_column="1"
                        app:layout_row="0">

                        <ImageButton
                            android:id="@+id/image_button4"
                            style="@android:style/Widget.Material.Light.ImageButton"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/imgbut_text4"
                            android:layout_alignParentStart="true"
                            android:layout_alignParentEnd="true"
                            android:layout_alignParentBottom="true"
                            android:layout_marginBottom="2dp"
                            android:background="#FFFFFF"
                            android:clickable="false"
                            android:contentDescription="Card Image"
                            app:layout_constraintBottom_toBottomOf="parent"
                            app:layout_constraintEnd_toEndOf="parent"
                            app:layout_constraintStart_toStartOf="parent"
                            app:layout_constraintTop_toBottomOf="@+id/imgbut_text4"
                            app:srcCompat="@drawable/ic_onair" />

                        <TextView
                            android:id="@+id/imgbut_text4"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="4dp"
                            android:fontFamily="@font/mukta_semibold"
                            android:text="@string/onair_tile"
                            android:textAlignment="center"
                            android:textColor="@color/blackcolor"
                            android:textSize="@dimen/home_card_font"
                            app:layout_constraintTop_toTopOf="parent" />

                    </androidx.constraintlayout.widget.ConstraintLayout>

                </LinearLayout>

尝试在home_activity.xml文件或需要包含的地方使用包含标记

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <include
        android:id="@+id/included_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        layout="@layout/include_tiles_layout"/>

</RelativeLayout>

这将创建单独的视图。

我希望代码放在一个新的布局文件夹中,我可以在“主页”活动中使用该文件夹,这样我就不必每次都在主页布局中提供属性。是否创建一个布局并重复使用?如果您使用include或merge标记,这会使操作变得简单。您不需要在每个布局中添加属性。我是否也可以使用这些标记向视图添加唯一ID?是的,您可以创建多个包含标记并单独使用。我在回答中加了一句
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:padding="100dp"
    >
    <include
        android:id="@+id/include_layout"     <!-- 아이디 설정 가능 -->
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/custom_timeset"      <!-- xml을 적는데 주의점 android빼고 -->
        />
    <include
        android:id="@+id/include_layout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/custom_timeset"
        />
    <include
        android:id="@+id/include_layout3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/custom_timeset"
        />
</LinearLayout>
View include01 = findViewById(R.id.include_layout);
View include02 = findViewById(R.id.include_layout2);
View include03 = findViewById(R.id.include_layout3);

TextView tv1 = (TextView) include01.findViewById(R.id.txt_startTime);
TextView tv2 = (TextView) include01.findViewById(R.id.txt_endTime);
TextView tv3 = (TextView) include02.findViewById(R.id.txt_startTime);
TextView tv4 = (TextView) include02.findViewById(R.id.txt_endTime);
TextView tv5 = (TextView) include03.findViewById(R.id.txt_startTime);
TextView tv6 = (TextView) include03.findViewById(R.id.txt_endTime);