简体   繁体   English

如何访问android xml布局文件中包含的视图内的元素?

[英]how to access elements inside an included view in a android xml layout file?

I created a layout and I included an android layout inside it : 我创建了一个布局,并在其中包含一个android布局:

<include
    layout="@android:layout/simple_list_item_multiple_choice" />

Once my whole view is inflated how can I access the element inside the simple_list_item_multiple_choice ? 放大整个视图后,如何访问simple_list_item_multiple_choice的元素? I guess there are a TextView and a CheckBox that I should be able to use but what's their id ? 我猜我应该可以使用TextView和CheckBox,但是它们的ID是什么?

EDIT: Do I need to inflate more than one thing ? 编辑:我是否需要充气一件事?

您可以通过以下方式访问它:

CheckedTextView ctv = (CheckedTextView)findViewById(android.R.id.text1);

I complete such tasks by providing an @+id to the <include/> statement. 我通过在<include/>语句中提供一个@+id来完成这些任务。 So for something like your example above I would write... 所以对于上面的示例,我会写...

  <include
  android:id="@+id/listItemMultiChoiceWrapper"
  layout="@android:layout/simple_list_item_multiple_choice" />

You could then access the text1 field by writing at your activity/fragment level (where ever you set the view containing the <include/> ) 然后,您可以通过在活动/片段级别进行书写来访问text1字段(无论您在何处设置包含<include/>的视图)

  final CheckedTextView ctv = (CheckedTextView) findViewById(id.listItemMultiChoiceWrapper).findViewById(id.text1);

If that doesn't work then check for duplicate ids confusing the situation (Lint should pick these up too). 如果这样不起作用,请检查重复的ID,以免混淆(Lint也应将其提取)。

This is the code for simple_list_item_multiple_choice from Android SDK: 这是来自Android SDK的simple_list_item_multiple_choice的代码:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:textAppearance="?android:attr/textAppearanceListItem"
    android:gravity="center_vertical"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple"
    android:paddingLeft="8dip"
    android:paddingRight="8dip"
/>

I guess you can handle it from here. 我想您可以从这里处理它。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM