我知道那里可能有一个非常简单的解决方案,但是对于我的一生,我似乎找不到它。如何更改RecycleView项目的宽度?由于某种原因,它显示的是父项的第一个完整宽度...它在每个项之间显示了很多空白。
<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>
CategoryAdapter:
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"/>
答案 0 :(得分:0)
我会将单只猫更改为
<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 :(得分:0)
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 :(得分:0)
最后,两天后,我在互联网上四处寻找,我自己弄清楚了。
确保您的 约束布局 为 wrap_content 。一直都是问题!