【问题标题】:RecyclerView: weird layoutRecyclerView:奇怪的布局
【发布时间】:2016-08-21 21:24:50
【问题描述】:

我将项目视图减少到最低限度,但我的布局仍然有很多填充,这不是我所期望的。

看照片:

渲染器中的一个单元格:

实际视图:

谁能解释我如何去除图像顶部和底部的这些空白?

这是代码(非常简单):

一个单元格

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

            <ImageView
                android:id="@+id/categoryPhoto"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/bg_alles_fur_kinder" />

            <TextView
                android:id="@+id/categoryName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Children"
                android:textColor="#fff"
                android:textAllCaps="true"
                android:layout_alignLeft="@id/categoryPhoto"
                android:layout_alignBottom="@id/categoryPhoto"
                android:textSize="22sp" />

</RelativeLayout>

自定义 RecyclerView

/**
 * Created by laurentmeyer on 21/08/16.
 */
public class CategoryRecyclerView extends RecyclerView {

    public CategoryRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setHasFixedSize(true);
        ArrayList<Category> categories = new ArrayList<>();
        categories.add(new Category(Category.CategoryType.CHILDREN));
        categories.add(new Category(Category.CategoryType.DECORATION));
        categories.add(new Category(Category.CategoryType.FASHION));
        categories.add(new Category(Category.CategoryType.FURNITURE));
        categories.add(new Category(Category.CategoryType.HOUSE));
        categories.add(new Category(Category.CategoryType.LITTERATURE));
        categories.add(new Category(Category.CategoryType.MULTIMEDIA));
        categories.add(new Category(Category.CategoryType.OTHER));
        CategoryRecyclerViewAdapter adapter = new CategoryRecyclerViewAdapter(categories);
        setAdapter(adapter);
        GridLayoutManager glm = new GridLayoutManager(getContext(), 2);
        glm.setAutoMeasureEnabled(true);
        setLayoutManager(glm);
    }

    public class CategoryRecyclerViewAdapter extends RecyclerView.Adapter<CategoryRecyclerViewAdapter.CategoryViewHolder> {

        List<Category> categories;

        CategoryRecyclerViewAdapter(List<Category> categories) {
            this.categories = categories;
        }


        @Override
        public CategoryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_card, parent, false);
            CategoryViewHolder cvh = new CategoryViewHolder(v);
            return cvh;
        }

        @Override
        public void onBindViewHolder(CategoryViewHolder holder, int position) {
            holder.categoryName.setText(categories.get(position).getType().getName());
            holder.photo.setImageResource(categories.get(position).getType().imageId);
        }

        @Override
        public void onAttachedToRecyclerView(RecyclerView recyclerView) {
            super.onAttachedToRecyclerView(recyclerView);
        }


        @Override
        public int getItemCount() {
            return categories.size();
        }

        public class CategoryViewHolder extends RecyclerView.ViewHolder {
            TextView categoryName;
            ImageView photo;


            public CategoryViewHolder(View itemView) {
                super(itemView);
                categoryName = (TextView) itemView.findViewById(R.id.categoryName);
                photo = (ImageView) itemView.findViewById(R.id.categoryPhoto);
            }
        }
    }


}

编辑:使用LinearLayoutManager 效果很好。

【问题讨论】:

  • 你真的需要在 LinearLayout 中嵌套一个 RelativeLayout 吗?嵌套布局不利于性能。
  • 好的,我可以删除它,之前有一个cardview,但我删除它是为了调试。谢谢
  • 现在仔细搜索您当前的主题/样式以查找填充/边距以及可能隐藏在 dimens.xml 文件中的内容。
  • 不,全部归零,什么都不改变
  • @SaranSankaran 可以,但这不是预期的行为,请参阅我的回答

标签: android android-layout android-recyclerview gridlayoutmanager


【解决方案1】:

我明白了!

如果您在ImageView 中使用:android:adjustViewBounds="true",它可以工作!

致谢:AppGuruz

【讨论】:

  • 太棒了。现在请接受您自己的答案,以便从未回答的问题队列中删除您的帖子。
猜你喜欢
  • 1970-01-01
  • 2022-12-14
  • 1970-01-01
  • 2014-02-25
  • 2014-11-10
  • 2014-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多