我正在使用回收站视图向我的视图动态添加项目。
这是 frame_layout ,我在其中定义回收站视图的尺寸
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:clickable="true"
tools:context=".CategoriesFragment">
<RelativeLayout
android:id="@+id/categories_fruits_clicked"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/categories_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
</FrameLayout>
这是 recycler_view_list_item
<?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"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="@drawable/product_background" <!--a blue colored background, with rounded corners-->
android:layout_margin="16dp">
<ImageView
android:id="@+id/product_imageView"
android:layout_marginTop="15dp"
android:layout_width="120dp"
android:layout_height="120dp"/>
<TextView
android:id="@+id/textViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"
android:layout_toEndOf="@id/product_imageView"
android:text="Banana"
android:fontFamily="@font/baloo"
android:textSize="20sp"
android:textColor="@color/colorAccent" />
<TextView
android:id="@+id/textViewPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textViewTitle"
android:layout_marginStart="16dp"
android:layout_toEndOf="@id/product_imageView"
android:text="Rs 120"
android:textStyle="bold"
android:textColor="@color/green"/>
<Button
android:id="@+id/qty_minus"
android:background="@null"
android:text="-"
android:textColor="@color/red"
android:textSize="20sp"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_below="@+id/textViewPrice"
android:layout_toEndOf="@id/product_imageView"
android:layout_margin="16dp"/>
<TextView
android:id="@+id/qty_counter"
android:text="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textViewPrice"
android:layout_toEndOf="@id/qty_minus"
android:layout_marginTop="26dp"
android:layout_marginEnd="10dp"/>
<Button
android:id="@+id/qty_plus"
android:background="@null"
android:text="+"
android:textColor="@color/red"
android:textSize="20sp"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_below="@+id/textViewPrice"
android:layout_toEndOf="@id/qty_counter"
android:layout_marginTop="16dp"/>
</RelativeLayout>
</LinearLayout>
出于某种奇怪的原因,回收站视图并未覆盖整个屏幕宽度,尽管将其定义为“ match_parent”。令人惊讶的是,预览显示正确,蓝色背景在布局宽度上分布,但是,在我的设备中,预览仅显示与包装内容一样大的内容。
当我将recycler_view_list_item的“相对布局”中的layout_width更改为400dp时,它将覆盖整个屏幕的宽度。 match_parent在哪里出问题?
修改1: 我的 ProductRecyclerViewAdapter
public class ProductRecyclerViewAdapter extends RecyclerView.Adapter<ProductRecyclerViewAdapter.ProductViewHolder> {
private Context context;
private List<Product> productList;
public ProductRecyclerViewAdapter(Context context, List<Product> productList) {
this.context = context;
this.productList = productList;
}
@NonNull
@Override
public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.product_recyclerview_list_item,null);
ProductViewHolder holder = new ProductViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ProductViewHolder prouctViewHolder, int i) {
Product product = productList.get(i);
prouctViewHolder.pdtTitle.setText(product.getTitle());
prouctViewHolder.pdtPrice.setText("MRP Rs " + String.valueOf(product.getPrice()));
prouctViewHolder.pdtImageView.setImageDrawable(context.getResources().getDrawable(product.getImageId()));
}
@Override
public int getItemCount() {
return productList.size();
}
class ProductViewHolder extends RecyclerView.ViewHolder{
ImageView pdtImageView;
TextView pdtTitle, pdtPrice;
public ProductViewHolder(@NonNull View itemView) {
super(itemView);
pdtImageView = itemView.findViewById(R.id.product_imageView);
pdtTitle = itemView.findViewById(R.id.textViewTitle);
pdtPrice = itemView.findViewById(R.id.textViewPrice);
}
}
}
编辑2:添加了图像
预览(上方)
在我的设备中(上面)该怎么看
答案 0 :(得分:0)
尝试此操作,将“相对”布局更改为线性布局,并将布局权重设置为1
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:clickable="true"
tools:context=".CategoriesFragment">
<LinearLayout
android:id="@+id/categories_fruits_clicked"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/categories_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
</RelativeLayout>
</FrameLayout>
答案 1 :(得分:0)
<?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="match_parent"
android:orientation="vertical">
使用项目布局的匹配父级高度和
LayoutInflater.from(getContext()).inflate(R.layout.recycler_view_list_item, parent, false)