java - 安卓 : No resource identifier found for attribute 'headerView'

标签 java android android-layout

使用 Android Studio,我在 Activity 布局中遇到此错误:错误:(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>

我非常确定布局 header_parallax_social.xml 在我的项目文件 (res/layout) 中可用,这是 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 和类似的东西,但仍然没有找到解决方案...

最佳答案

您必须在 values 文件夹中的 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 中显示标题 View ,您必须在自定义 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();
        }
    }
}

如果您想避免上述努力,您可以通过编程方式将标题 View 设置为 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);

希望这对您有所帮助。

关于java - 安卓 : No resource identifier found for attribute 'headerView' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34920570/

相关文章:

java - 无限循环,不会以while循环结束

java - 无法运行 Apache James

java - 这哪里出了问题?线程中的异常 "main"java.lang.ArrayIndexOutOfBoundsException : 5

用于播放音乐的 Android 服务 : why START_STICKY?

java - Android类被调用两次

android - Android checkSelfPermission()中的 "this"指的是什么?

java - 性能基准测试属于 JUnit 领域吗?

android-为 Chip 文本添加顶部填充

android-layout - 来自 View 适配器的无条件布局膨胀。 Kotlin

java - onCreateContextMenu 未被调用