我已经在Android开发人员上阅读了Android UI技巧2,它告诉人们如何多次在另一个布局文件中包含布局,并为这些布局提供不同的ID.但是,此处的示例是覆盖布局ID,而不是此布局中视图的ID.例如,如果workspace_screen.xml如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView android:id="@+id/firstText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="first"/>
<TextView android:id="@+id/secondText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="second"/>
Run Code Online (Sandbox Code Playgroud)
我在另一个布局文件中包含了三次.我最终得到三个带有id firstText的TextView,还有另外三个带有secondText的TextView?是不是有碰撞?如何在findViewById的第三个包含布局中找到secondText TextView?我应该在findViewById方法中输入什么?
我有第一个和第二个布局,我想先插入第二个.我想将第二个布局插入到具有id @ + id/layout的布局中,当我按下按钮获取布局时,它会显示按钮底部的第二个布局
第一个布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_get_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Layout" />
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
第二个布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/card_base"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/img_cover"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
android:scaleType="fitXY"
android:src="@drawable/card_beauty" />
<ImageView
android:id="@+id/img_photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="20dp"
android:scaleType="fitXY"
android:src="@drawable/sample_0" />
</RelativeLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)