GridView项目起床

时间:2019-01-29 12:55:16

标签: android android-layout

我遇到了这个问题,尽管您添加了“ 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);
    }
}
}

enter image description here

感谢您的任何帮助,谢谢

3 个答案:

答案 0 :(得分:2)

您应该将textview的引力设置为底部,并为imageview指定layout_above,这样可以尝试尝试

答案 1 :(得分:1)

您需要在您的 ImageView 中添加margintop

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

答案 2 :(得分:0)

layout_below会将标记的布局放置在引用的布局下方,并且不会将其放置在父级的底部。要将视图与父视图的底部对齐,请使用android:layout_alignParentBottom="true"并根据需要调整其他布局。