首页 > 解决方案 > GridView 项目起床

问题描述

我有这个问题,而您可以在图像中看到项目正在上升很远,即使我添加了“layout_below”并且我不知道它为什么会发生或如何修复它,我可以通过添加填充来部分修复它但我不想用这种方法修复,我想要一个通用的修复

<RelativeLayout 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"
tools:context=".ShoppingPackage.ProductsListActivity">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="16dp"
    android:id="@+id/IntroLayout">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:text="@string/browse"
        android:textSize="36sp"
        android:textColor="@android:color/black"
        android:id="@+id/welcome_back"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/welcome_back"
        android:text="@string/products_list"
        android:textColor="@android:color/black"
        android:textSize="12sp"
        android:id="@+id/categoryName"/>



</RelativeLayout>


<GridView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/IntroLayout"
    android:id="@+id/productsrecycler"
    android:background="#3C3C3C"
    android:numColumns="2"/>

 </RelativeLayout>

这是第二个 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="5dp"
app:cardElevation="10dp"
app:cardBackgroundColor="#222222">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="180dp"
        android:layout_height="180dp"
        android:id="@+id/productImageView"
        android:scaleType="fitXY"
        android:layout_centerHorizontal="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/product_name"
        android:id="@+id/productName"
        android:layout_below="@id/productImageView"
        android:layout_centerHorizontal="true"
        android:textSize="18sp"
        android:textColor="@android:color/white"/>


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/product_price"
        android:id="@+id/productPrice"
        android:layout_below="@id/productName"
        android:layout_centerHorizontal="true"
        android:textColor="@android:color/white" />


</RelativeLayout>



</android.support.v7.widget.CardView>

这是适配器类:

public class PRecyclerViewAdapter extends BaseAdapter {

private ArrayList<PModel> products = new ArrayList<>();
private static final String TAG = "PRecyclerViewAdapter";
private PAdapterCallback callback;
private Context context;

public PRecyclerViewAdapter(Context context) {this.context = context;}

public void setOnShareClickedListener(PAdapterCallback adapterCallback) {
    this.callback = adapterCallback;
}

@Override
public int getCount() {
    return products.size();
}

@Override
public Object getItem(int position) {
    return products.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final PModel product = products.get(position);
    ViewHolder viewHolder;

    convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.productlistlayout, null);
    viewHolder = new ViewHolder(convertView, product);
    convertView.setTag(viewHolder);

    convertView.setOnClickListener(v -> callback.pClicked(product));

    convertView.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT, 700));
    return convertView;
}

public interface PAdapterCallback{
    void pClicked(PModel product);
}

public void setProductList(ArrayList<PModel> products) {
    this.products = products;
    Log.e(TAG, "setProductList: "+products);
    this.notifyDataSetChanged();
}




class ViewHolder{

    TextView productName;
    TextView productPrice;
    ImageView productImageView;
    ViewHolder(@NonNull View itemView, PModel product) {


        productName = itemView.findViewById(R.id.productName);
        productPrice = itemView.findViewById(R.id.productPrice);
        productImageView = itemView.findViewById(R.id.productImageView);

   Glide.with(context).load(product.getImagelinks().get(0)).into(productImageView);
        productName.setText(product.getProductname());

        DecimalFormatSymbols otherSymbols = new 
        DecimalFormatSymbols(Locale.US);
        DecimalFormat df = new DecimalFormat("#.##########", otherSymbols);
        String price = df.format(product.getPrice())+ " "+CURRENCY;

        productPrice.setText(price);
    }
}
}

在此处输入图像描述

感谢您的帮助

标签: androidandroid-layout

解决方案


您应该将 textview 的重力设置为底部并为 imageview 指定 layout_above 它将起作用尝试一下


推荐阅读