更改 RecyclerView 项目宽度?
Change RecycleView item width?
我知道那里可能有一个非常简单的解决方案,但我似乎无法找到它。如何更改 RecycleView 项目的宽度?出于某种原因,它显示父项的第一个全宽...它在每个项目之间显示很多白色 space。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_wrapper"
android:background="@color/colorPrimary"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:padding="20dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Categories"
android:textColor="#ffffff"
android:layout_marginBottom="10dp"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cats"
></android.support.v7.widget.RecyclerView>
</LinearLayout>
</LinearLayout>
类别适配器:
public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> {
private ArrayList<Category> categories;
public CategoryAdapter(ArrayList<Category> categories) {
this.categories = categories;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_cat, parent, false);
return new ViewHolder(v);
}
@Override public void onBindViewHolder(ViewHolder holder, int position) {
Category cat = this.categories.get(position);
holder.text.setText(cat.getText().toString());
}
@Override public int getItemCount() {
return categories.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView text;
public ViewHolder(View itemView) {
super(itemView);
text = (TextView) itemView.findViewById(R.id.cat_title);
}
}
}
single_cat.xml
<TextView
android:layout_width="100dp"
android:layout_height="40dp"
android:text="Cat"
android:background="@drawable/cat_single_shape"
android:padding="10dp"
android:gravity="center"
android:id="@+id/cat_title"
android:textSize="13sp"
android:fontFamily="@font/open_sans_bold"
android:textColor="@color/colorPrimary"/>
我会把单只猫改成
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/cat_single_shape"
android:padding="10dp"
android:gravity="center"
android:id="@+id/cat_title"
android:textSize="13sp"
android:fontFamily="@font/open_sans_bold"
android:textColor="@color/colorPrimary"/>
1) 在你的片段中做:
recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.HORIZONTAL, true));
2) 对于 recyclerView 单个项目。xml 做:
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_margin="4dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
</LinearLayout>
3) 并将 RecyclerView 更改为:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_wrapper"
android:background="@color/colorPrimary"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.2"
android:text="Categories"
android:textColor="#ffffff"
android:layout_marginBottom="10dp"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.8"
android:id="@+id/cats"/>
</LinearLayout>
终于在网上找了2天,自己搞定了。
确保您的约束布局是wrap_content。这一直是个问题!
我知道那里可能有一个非常简单的解决方案,但我似乎无法找到它。如何更改 RecycleView 项目的宽度?出于某种原因,它显示父项的第一个全宽...它在每个项目之间显示很多白色 space。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_wrapper"
android:background="@color/colorPrimary"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:padding="20dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Categories"
android:textColor="#ffffff"
android:layout_marginBottom="10dp"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cats"
></android.support.v7.widget.RecyclerView>
</LinearLayout>
</LinearLayout>
类别适配器:
public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> {
private ArrayList<Category> categories;
public CategoryAdapter(ArrayList<Category> categories) {
this.categories = categories;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_cat, parent, false);
return new ViewHolder(v);
}
@Override public void onBindViewHolder(ViewHolder holder, int position) {
Category cat = this.categories.get(position);
holder.text.setText(cat.getText().toString());
}
@Override public int getItemCount() {
return categories.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView text;
public ViewHolder(View itemView) {
super(itemView);
text = (TextView) itemView.findViewById(R.id.cat_title);
}
}
}
single_cat.xml
<TextView
android:layout_width="100dp"
android:layout_height="40dp"
android:text="Cat"
android:background="@drawable/cat_single_shape"
android:padding="10dp"
android:gravity="center"
android:id="@+id/cat_title"
android:textSize="13sp"
android:fontFamily="@font/open_sans_bold"
android:textColor="@color/colorPrimary"/>
我会把单只猫改成
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/cat_single_shape"
android:padding="10dp"
android:gravity="center"
android:id="@+id/cat_title"
android:textSize="13sp"
android:fontFamily="@font/open_sans_bold"
android:textColor="@color/colorPrimary"/>
1) 在你的片段中做:
recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.HORIZONTAL, true));
2) 对于 recyclerView 单个项目。xml 做:
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_margin="4dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
</LinearLayout>
3) 并将 RecyclerView 更改为:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_wrapper"
android:background="@color/colorPrimary"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.2"
android:text="Categories"
android:textColor="#ffffff"
android:layout_marginBottom="10dp"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.8"
android:id="@+id/cats"/>
</LinearLayout>
终于在网上找了2天,自己搞定了。
确保您的约束布局是wrap_content。这一直是个问题!