多次重用布局时如何访问布局内的视图? [英] How do I access the views inside the layout when I reuse it multiple times?

查看:19
本文介绍了多次重用布局时如何访问布局内的视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了 Android 开发人员的 Android UI 技巧 2,它告诉人们如何多次将布局包含在另一个布局文件中,并为这些包含的布局赋予不同的 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"/>

我在另一个布局文件中包含了三次.我最终会得到三个 ID 为 firstText 的 TextViews,另外三个带有 secondText 的 TextViews 吗?不是有id冲突吗?以及如何使用 findViewById 在第三个包含的布局中找到 secondText TextView?我应该在 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>

所以在你的代码中,你像这样插入它:

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

请注意,您使用各个 LinearLayouts 的 findViewById 方法将搜索范围缩小到仅其子项.

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

这篇关于多次重用布局时如何访问布局内的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆