Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
237 views
in Technique[技术] by (71.8m points)

android - How do I access the views inside the layout when I reuse it multiple times?

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Say you want to include this:

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

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>

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");

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...