2016-01-21 47 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

相关问题