Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.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中,recyclerview项不在中心位置?_Android_Android Recyclerview - Fatal编程技术网

为什么在Android中,recyclerview项不在中心位置?

为什么在Android中,recyclerview项不在中心位置?,android,android-recyclerview,Android,Android Recyclerview,我正在为Android开发recycleView,我尝试将项目集中在recycle中,如下代码所示 <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:aap

我正在为Android开发
recycleView
,我尝试将项目集中在recycle中,如下代码所示

<?xml version="1.0" encoding="utf-8"?>
<layout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:aapt="http://schemas.android.com/aapt">


    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white"
            android:orientation="vertical">


        <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recycleview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"/>



        <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_margin="10dp"
                android:gravity="center">

            <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/test"/>

            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>

        </LinearLayout>


    </LinearLayout>

</layout>
并使用线性布局,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<layout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:aapt="http://schemas.android.com/aapt">

    <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:background="@color/blue">

        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="123"/>

    </LinearLayout>
</layout>
        val adapter = ShowAdapter(viewModel)
        val layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
        binding.recycleview.layoutManager = layoutManager
        binding.recycleview.adapter = adapter
        adapter.notifyDataSetChanged()
但它显示如下:

按钮不在中间,我是不是遗漏了什么?
感谢ion advance。

也许您的
线性布局中缺少重力布局

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white" 
        //here it is
        android:gravity="center"
        android:orientation="vertical">

请检查下面的代码。我测试了它,它工作正常

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">


            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycleview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal" />


            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:gravity="center"
                android:orientation="horizontal">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                     />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </LinearLayout>


        </LinearLayout>

    </LinearLayout>

用于适配器项

  <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/black">

            <Button
                android:id="@+id/btn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="123"/>

        </RelativeLayout>
    </LinearLayout>

适配器类

public class TestAdapter extends
        RecyclerView.Adapter<TestAdapter.ViewHolder> {

    public Context mContext;
    private List<String> orderList;


    public TestAdapter(List<String> orders,
                       Context context) {
        this.orderList = orders;
        this.mContext = context;

        try {
        } catch (ClassCastException e) {
            throw new ClassCastException(mContext.toString()
                    + " must implement OnAccountPermissionChanged");
        }
    }

    @Override
    public TestAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

        View view = LayoutInflater.from(viewGroup.getContext()).inflate(
                R.layout.item, viewGroup, false);
        return new TestAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int i) {

        String t = orderList.get(i);
        viewHolder.textView.setText(t);

    }


    @Override
    public int getItemCount() {
        return orderList == null ? 0 : orderList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        public Button textView;


        public ViewHolder(View view) {
            super(view);
            textView = view.findViewById(R.id.btn);


        }
    }


}
公共类TestAdapter扩展
RecyclerView.适配器{
公共语境;
私有列表订单列表;
公共测试适配器(列表命令,
上下文(上下文){
this.orderList=订单;
this.mContext=上下文;
试一试{
}catch(ClassCastException e){
抛出新的ClassCastException(mContext.toString()
+“必须实施OnAccountPermissionChanged”);
}
}
@凌驾
public TestAdapter.ViewHolder onCreateViewHolder(ViewGroup ViewGroup,int viewType){
View=LayoutFlater.from(viewGroup.getContext())。充气(
R.layout.item,视图组,false);
返回NewTestAdapter.ViewHolder(视图);
}
@凌驾
public void onBindViewHolder(最终ViewHolder ViewHolder,最终int i){
字符串t=orderList.get(i);
viewHolder.textView.setText(t);
}
@凌驾
public int getItemCount(){
return orderList==null?0:orderList.size();
}
公共类ViewHolder扩展了RecyclerView.ViewHolder{
公共按钮文本视图;
公共视图持有者(视图){
超级(视图);
textView=view.findviewbyd(R.id.btn);
}
}
}
活动代码

 recyclerView=findViewById(R.id.recycleview);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        List<String> test=new ArrayList<>();
        test.add("aa");
        test.add("aa");
        test.add("aa");
        test.add("aa");
        TestAdapter adapter=new TestAdapter(test,this);
        recyclerView.setAdapter(adapter);
recycleview=findViewById(R.id.recycleview);
recyclerView.setLayoutManager(新的LinearLayoutManager(本));
列表测试=新建ArrayList();
测试。添加(“aa”);
测试。添加(“aa”);
测试。添加(“aa”);
测试。添加(“aa”);
TestAdapter适配器=新的TestAdapter(测试,本);
recyclerView.setAdapter(适配器);

改变部件的重力,有两种方法:

1) 使用
android:gravity=“center”
请注意:您的所有孩子都会受到影响并处于中心位置

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white" 
        //here it is
        android:gravity="center"
        android:orientation="vertical">
另外,我看到您正在使用androidx。最好使用
MaterialButton
而不是
Button

<com.google.android.material.button.MaterialButton
    android:id="@+id/material_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="123"/>


请查看此

无需如此复杂,请使用
框架布局
,因为它更具性能


如果由于某种原因,您的项目布局只有一个按钮,您甚至可以删除
FrameLayout
,并将
MaterialButton
直接用作根元素


重要的属性是android:layout\u gravity=“center”

如果切换到
LinearLayout
,则可以更改根目录的
android:gravity
属性:



在最后一种情况下,重要的是保持包裹内容的宽度(如果垂直,则保持高度)。

使用此布局,它将在项目中心显示按钮

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="wrap_content"
        android:background="@color/blue">

    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="123"/>

</LinearLayout>


您有两个线性布局尝试为第一个添加重力,因为它是RecycleView的父对象请在检查缺少的是
?请再次检查我的答案,我共享了完整的代码,它可以正常工作me@Wun你检查过这个解决方案吗?
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="wrap_content"
        android:background="@color/blue">

    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="123"/>

</LinearLayout>