【问题标题】:match_parent is working as wrap_content for some weird reasonmatch_parent 出于某种奇怪的原因作为 wrap_content 工作
【发布时间】:2019-06-25 18:36:33
【问题描述】:

我正在使用回收站视图将项目动态添加到我的视图中。

这是 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:添加图片

预览(上)

如何在我的设备中看到(上图)

【问题讨论】:

  • 你能把列表项没有覆盖全屏的截图贴出来吗?您还提到了相对布局的 layout_margin 可能是问题所在..
  • 把 android:layout_margin="16dp" 这行从相对布局中去掉再试试。
  • 分享你的布局膨胀代码
  • 对于膨胀布局,请使用您的 viewGroup 而不是 null 喜欢:inflater .inflate(R.layout.product_recyclerview_list_item,viewGroup,false);而对于充气机 LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());

标签: android


【解决方案1】:

试试这个,将您的相对布局更改为线性布局并将布局权重设置为 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>

【讨论】:

  • 更改所有 match_parent 以包装除 Recycleview 之外的内容。玩这个。
【解决方案2】:
<?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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-07
    • 2013-03-16
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 2013-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多