如何访问里面的布置的意见,当我重新使用它多次?当我、里面、意见、使用它

由网友(独来独往)分享简介:我已经阅读了Android UI招2在Android开发者,它告诉人们如何将在另一个布局文件多次布局,给这些包括布局不同的ID。然而,该样本这里是覆盖布局ID,而不是在这个布局中的视图ID。例如,如果workspace_screen.xml看起来是这样的:I have read the Android UI tric...

我已经阅读了Android UI招2在Android开发者,它告诉人们如何将在另一个布局文件多次布局,给这些包括布局不同的ID。然而,该样本这里是覆盖布局ID,而不是在这个布局中的视图ID。例如,如果workspace_screen.xml看起来是这样的:

I have read the Android UI trick 2 on Android developers, which tells people how to include a layout in another layout file multiple times, and give these included layouts different id. However, the sample here is overwriting the layout id, not the id of the views IN this layout. For example, if the workspace_screen.xml looks like this:

<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"/>

和我将其包含在另一个布局文件三次。难道我最终有三个TextViews id为firstText,和另外三个与secondText?是不是有一个ID冲突?我如何找到secondText的TextView与findViewById第三列入布局?我应该投入的findViewById方法?

And I include it three times in another layout file. Do I end up with three TextViews with id firstText, and another three with secondText? Isn't there an id collision? And how do I find the secondText TextView in the third included layout with findViewById? What should I input in the findViewById method?

推荐答案

说要包括这样的:

<LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:orientation="horizontal"
>
    <ImageView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/some_image"
    />
    <TextView
        android:id="@+id/included_text_view"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
    />
</LinearLayout>

所以在你的code插入这样的:

so in your code you insert it like this:

<LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:orientation="vertical"
>
    <include android:id="@+id/header_1" layout="@layout/name_of_layout_xml" />
    <include android:id="@+id/header_2" layout="@layout/name_of_layout_xml" />
</LinearLayout>

现在您要访问的包括布局中的文本的意见,以动态地设置文本。在您的code,你只需键入:

now you want to access the text views within the included layouts to set the text dynamically. In your code you simply type:

LinearLayout ll = (LinearLayout)findViewById(R.id.header_1);
TextView tv = (TextView)ll.findViewById(R.id.included_text_view);
tv.setText("Header Text 1");

ll = (LinearLayout)findViewById(R.id.header_2);
tv = (TextView)ll.findViewById(R.id.included_text_view);
tv.setText("Header Text 2");

您使用个人LinearLayoutsfindViewById方法来缩小搜索范围,只有自己的孩子

的通知。

notice that you use the individual LinearLayouts' findViewById methods to narrow the search to only their children.

阅读全文

相关推荐

最新文章