Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android LayoutFlater忽略XML属性_Android_Xml_Android Recyclerview_Adapter_Layout Inflater - Fatal编程技术网

Android LayoutFlater忽略XML属性

Android LayoutFlater忽略XML属性,android,xml,android-recyclerview,adapter,layout-inflater,Android,Xml,Android Recyclerview,Adapter,Layout Inflater,我的公寓有问题。 首先是my item.xml: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="100dp" android:layout_height=&qu

我的公寓有问题。 首先是my item.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:background="@drawable/button" >
 
</RelativeLayout>
一切正常,看起来我希望它是:

在许多关于LayouInflater的文档和文章中,他们建议如下使用LayouInflater:

View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, null);
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
@NonNull
@Override
public TestViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(mContext).inflate(R.layout.item, parent, false);
    RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    v.setLayoutParams(lp);
    return new TestViewHolder(v);
}
但当我这样做时,它会忽略item.xml文件中的属性:

有很多文章解释应该避免使用第一种方法(为我工作的方法)。 以下是其中一篇文章:

这是我的完整RecyclerView.Adapter类:

public class AdapterTest extends RecyclerView.Adapter<AdapterTest.TestViewHolder> {


    @Override
    public TestViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, null);
        return new TestViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final TestViewHolder holder, int position) {


    }

    @Override
    public int getItemCount() {
        return 10;
    }

    public class TestViewHolder extends RecyclerView.ViewHolder {
 

        public TestViewHolder(View itemView) {
            super(itemView); 
        }
    }


}
公共类适配器测试扩展了RecyclerView.Adapter{
@凌驾
公共TestViewHolder onCreateViewHolder(视图组父级,int-viewType){
视图v=LayoutInflater.from(parent.getContext()).flate(R.layout.item,null);
返回新的TestViewHolder(v);
}
@凌驾
public void onBindViewHolder(最终测试ViewHolder,int位置){
}
@凌驾
public int getItemCount(){
返回10;
}
公共类TestViewHolder扩展了RecyclerView.ViewHolder{
公共TestViewHolder(视图项视图){
超级(项目视图);
}
}
}

如何使用推荐的方法,但仍能获得所需的结果?

我通过在onCreateViewHolder中添加以下两行来修复此问题:

RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
v.setLayoutParams(lp);
我的onCreateViewHolder现在看起来像这样:

View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, null);
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
@NonNull
@Override
public TestViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(mContext).inflate(R.layout.item, parent, false);
    RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    v.setLayoutParams(lp);
    return new TestViewHolder(v);
}
以下是我的答案:

我真的无法解释为什么它现在可以工作,因为我的RecyclerView从一开始就设置为MatchParent width和hight。
我很高兴它现在可以工作了

是我自己还是这些图片都是一样的?@SlothCoding My bad,更新了第二张图片现在尝试在根视图的item.xml上使用边距,在您的情况下,它是相对的。这样,每个项目之间都会有空间。然后选择第二个选项。@SlothCodeing现在修复了重叠,但仍然有一个奇怪的行为,它在右边有0个边距,但在左边有很多边距。@SlothCodeing感谢您尝试帮助我,我找到了一个解决方案并发布了答案。