我有几个图块,我home_activity.xml
想为图块制作一个自定义视图,这样就不必为每个图块单独编写每个属性。
这是用于在home活动的每个图块中添加组件的代码段,我想为其创建自定义视图。我提供了两个这样的图块的示例。
<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文件(或需要包含的位置)中使用include标记。
<?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>
[ps]使多个包含。
例如,在home_activity.xml中
<?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>
在java文件中
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);
这使看法分开。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句