java – Android:找不到属性’headerView’的资源标识符

使用 Android Studio,我在活动布局中收到此错误:错误:(9)在“com.merahjambutech.zuki.deteksi”包中找不到属性“headerView”的资源标识符

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    xmlns:compat="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <com.merahjambutech.zuki.deteksi.view.PullToZoomListViewEx
        android:id="@+id/paralax_social_list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@android:color/transparent"
        app:headerView="@layout/header_parallax_social" />
</LinearLayout>

我非常确定我的项目文件(res / layout)中的布局header_parallax_social.xml是可用的,这里是header_parallax_social的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="160dp" >

    <ImageView
        android:id="@+id/header_parallax_social_new_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_centerVertical="true"
        android:layout_gravity="center_horizontal"
        android:contentDescription="@string/cd_main_image"
        android:scaleType="centerCrop"
        android:src="@drawable/parallax_social_small" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:orientation="vertical" >
    </LinearLayout>

</RelativeLayout>

我试图改变xmlns:app和类似的东西,但仍未找到解决方案……

最佳答案 您必须在值文件夹的attrs.xml中为Listview设置自定义属性,即headerView:

attrs.xml

 <resources>
<declare-styleable name="PullToZoomListViewEx">  declare your custom listview class name here
    <attr name="headerView" format="reference"/>
</declare-styleable>
 </resources>

通过这样做我希望app:headerView =“@ layout / header_parallax_social”不会显示任何错误,但要在列表视图中显示标题视图,您必须在自定义Listview类中进行一些更改,它应该看起来像

public class PullToZoomListViewEx  extends ListView{
private int headerId;
public PullToZoomListViewEx(Context context) {
    super(context);
    }

  public PullToZoomListViewEx(Context context, AttributeSet attrs) {
      this(context, attrs, 0);

    }

  public PullToZoomListViewEx(Context context, AttributeSet attrs, int  defStyle) {
        super(context, attrs, defStyle);
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PullToZoomListViewEx, defStyle, defStyle);

        try {
            headerId = a.getResourceId(R.styleable.PullToZoomListViewEx_headerView, View.NO_ID);
            if (headerId != View.NO_ID) {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View header = inflater.inflate(headerId, null);
                addHeaderView(header);
            }
        } finally {
            a.recycle();
        }
    }
}

要么

如果您想避免上述工作,可以通过编程方式将标题视图设置为Listview,如下所示:

   LayoutInflater inflater = getLayoutInflater();
   ViewGroup header = (ViewGroup)inflater.inflate(R.layout.header,myListView, false);
//replace  R.layout.header with R.layout.header_parallax_social and  myListView with your listview object
   myListView.addHeaderView(header, null, false);

希望这可以帮助.

点赞