Android:找不到属性“headerView”的资源标识符

时间:2016-01-21 10:02:03

标签: java android android-layout

使用Android Studio,我的活动布局中出现此错误:Error:(9) No resource identifier found for attribute 'headerView' in package 'com.merahjambutech.zuki.deteksi'

<?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以及类似的东西,但仍未找到解决方案......

1 个答案:

答案 0 :(得分:1)

您必须在Listview文件夹的attrs.xml中为values设置自定义属性,即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);

希望这有帮助。