簡體   English   中英

Android:找不到屬性“headerView”的資源標識符

[英]Android : No resource identifier found for attribute 'headerView'

使用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和類似的東西,但仍未找到解決方案......

你必須設置自定義屬性即headerView您的Listviewattrs.xmlvalues的文件夾:

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);

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM