简体   繁体   English

如何在android中重用布局,但同时使用多个ID?

[英]How to reuse layout in android but at the same time use multiple ID?

http://developer.android.com/training/improving-layouts/reusing-layouts.html http://developer.android.com/training/improving-layouts/reusing-layouts.html

In this website it introduce 本网站介绍

<include layout="@layout/titlebar"/>

to reuse layout, so I may code like this the problem is , 重用布局,所以我可能会这样编码,问题是

<include layout="@layout/titlebar"
 android:id="@+id/bar_1"/>

<include layout="@layout/titlebar"
 android:id="@+id/bar_2"/>

if the titlebar is a linearlayout, and I would like to get the textview inside titlebar , how can I differenate between bar 1 and bar 2? 如果标题栏是线性布局,并且我想在titlebar中获取textview ,如何区分栏1和栏2? Thanks 谢谢

Try: 尝试:

// Get root View id from that include link
View yourLayout1 = findViewById(R.id.bar1); 
View yourLayout2 = findViewById(R.id.bar2); 

// Get text view contained inside the include file
TextView yourTextView1 = (TextView)(yourLayout1.findViewById( R.id.yourInnerTextview )); 
TextView yourTextView2 = (TextView)(yourLayout2.findViewById( R.id.yourInnerTextview ));

PS: I haven't tested it but logically sounds good. PS:我还没有测试过,但是从逻辑上讲还不错。 So let me know if it works. 所以,让我知道它是否有效。

/**
 * Look for a child view with the given id.  If this view has the given
 * id, return this view.
 *
 * @param id The id to search for.
 * @return The view that has the given id in the hierarchy or null
 */
public final View findViewById(int id) {
    if (id < 0) {
        return null;
    }
    return findViewTraversal(id);
}

View Object also have findViewById function. 视图对象还具有findViewById函数。 And it only find the child . 而且它只能找到孩子。 So you can firstly find the bar_1 or bar_2 , and then use the bar_1 Object or bar_2 Object's findViewById function to get the child view you want. 因此,您可以首先找到bar_1或bar_2,然后使用bar_1对象或bar_2对象的findViewById函数来获取所需的子视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在 Android 中使用 ViewBinding 在单个片段中使用具有相同 ID 的多个布局 - How can i use Multiple layout with same ID's in a Single fragment using ViewBinding in Android 如何在Android Studio中的多个活动/片段中重用相同的xml背景布局? - How do I reuse the same xml background layout in multiple activities/fragments in Android Studio? 如何同时为Android布局元素的背景使用形状和选择器? - How to use a shape and selector at the same time for the background of a android layout element? 如何以线性顺序重用多个活动和布局进行测验(Android) - How to reuse multiple activities and layout in linear order for quiz (android) 如何在Android中重用线性布局? - How to reuse linear layout in android? android:如何回收/重用布局? - android: How to recycle/reuse a layout? 多次添加同一视图-重复使用布局 - Add same view multiple times - Reuse a layout Android:多个类可使用相同的XML布局 - Android: Multiple Classes to use same XML Layout 如何在膨胀的布局中膨胀具有相同 id 的布局的多个实例 - How to inflate multiple instances of a layout with the same id inside an inflated layout 我可以在 Android 的不同布局中使用相同的 id 吗? - Can I use the same id in different layout in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM