RecyclerView:奇怪的布局
RecyclerView: weird layout
我将我的项目视图减少到最低限度,但我的布局仍然有很多填充,这不是我所期望的。
看照片:
渲染器中的一个单元格:
实际观看:
谁能解释一下如何删除图像顶部和底部的这些空白区域?
这是代码(非常简单):
一个单元格
<?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
效果很好。
我明白了!
如果您在 ImageView
中使用:android:adjustViewBounds="true"
就可以了!
感谢:AppGuruz
我将我的项目视图减少到最低限度,但我的布局仍然有很多填充,这不是我所期望的。
看照片:
渲染器中的一个单元格:
实际观看:
谁能解释一下如何删除图像顶部和底部的这些空白区域?
这是代码(非常简单):
一个单元格
<?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
效果很好。
我明白了!
如果您在 ImageView
中使用:android:adjustViewBounds="true"
就可以了!
感谢:AppGuruz