2016-01-21 45 views
4

使用Android Studio中,我得到這個錯誤在我的活動佈局:Error:(9) No resource identifier found for attribute 'headerView' in package '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> 

我很肯定的佈局header_parallax_social.xml可在我的項目文件(RES /佈局),這裏有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:應用程序和類似的東西,但仍沒有找到解決方案...

+0

我想你應該使用drawable而不是layout。=>'app:headerView =「@ drawable/header_parallax_social」' –

+0

或者嘗試更改'xmlns:app =「http://schemas.android.com/apk/ RES-auto'到'的xmlns:程序=「HTTP:// schemas.android.com/tools' –

回答

1

你必須設置自定義的values文件夾中attrs.xml屬性即headerView您Listview

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"會沒有顯示出任何錯誤,但顯示在列表視圖標題視圖,你必須做你的自定義列表視圖類的一些變化,它應該看起來像

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

如果你想避免上面的努力,您可以在標題視圖編程設置這樣的列表視圖:

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

希望這有助於。

+0

謝謝,它的工作原理! – anunixercoder

相關問題