如何通过我的活动访问我的次要布局项目



我有两个XML布局用于一个活动。在主布局文件中,我定义了一个列表视图,以及此列表视图项的看起来像第二个布局定义。

这是具有列表视图的主要布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    tools:context=".MainActivity" >
    <ListView
        android:id="@+id/mylistView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        tools:listitem="@android:layout/simple_list_item_1"
        android:divider="@android:drawable/divider_horizontal_bright" >
    </ListView>
</RelativeLayout>

这是定义列表项的样子的第二个布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="10dp"
    android:gravity="center_vertical"
    tools:context=".MainActivity" >
    <TextView
        android:id="@+id/list_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:gravity="center_vertical" />
    <ImageView
        android:id="@+id/list_imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="20dp"
        android:layout_marginTop="22dp"
        android:layout_marginBottom="20dp" />
</RelativeLayout>

在主动脉中,我将主布局设置为内容视图。现在,我想访问第二个布局项目,例如文本视图和图像视图,但是使用layoutinflater不是一个选择。因为我已经尝试过,并且它不起作用,或者我不知道如何编写正确的语法。如何通过其ID或其他内容访问这些项目?谢谢。

如果要自定义文本视图和图像视图,具体取决于应该实现自定义适配器

的内容

这是

的方式

遵循简单的教程,我确定您的问题将得到解决。

如果没有,请提供更多信息,以及为什么要访问它

编辑:

这取决于您要在哪里访问它。例如,如果您在项目中单击方法中,则可以访问此视图

public void onItemClick(AdapterView<?> a, View v, int position,                     long id) {
    if (viewItemAnterior != null)
    {
    Button boton = (Button)v.findViewById(R.id.list_boton);
}
}

请参阅本教程,我认为它具有您正在闲逛的东西。listView的Android自定义适配器

最新更新