【问题标题】:Remove Excess Space in Gridview删除 Gridview 中的多余空间
【发布时间】:2020-01-06 20:05:40
【问题描述】:

Current Gridview

我希望第三张图片占据空白区域。当图像的高度相等时,gridview 看起来不错,但当它不相等时,它显示为空白以匹配下一个图像的高度。我只是想让下面的图片出现并填满空间。

适配器

public class Adapter extends BaseAdapter {
Context context;
int logos[];
LayoutInflater inflter;
public Adapter(Context applicationContext, int[] logos) {
    this.context = applicationContext;
    this.logos = logos;
    inflter = (LayoutInflater.from(applicationContext));
}
@Override
public int getCount() {
    return logos.length;
}
@Override
public Object getItem(int i) {
    return null;
}
@Override
public long getItemId(int i) {
    return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    view = inflter.inflate(R.layout.custom_grid, null); // inflate the layout
    ImageView icon = view.findViewById(R.id.icon); // get the reference of ImageView
    icon.setImageResource(logos[i]); // set logo images
    return view;
}

}

自定义视图

<?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:padding="1dp"
android:orientation="vertical">
<ImageView
    android:id="@+id/icon"
    android:scaleType="center"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:src="@drawable/logo" />
</LinearLayout>

XML

    <LinearLayout
    android:padding="5dp"
    android:orientation="vertical"
    android:background="@color/white"
    android:layout_below="@id/appbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <GridView
        android:stretchMode="columnWidth"
        android:numColumns="2"
        android:id="@+id/grid"
        android:gravity="center"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

Java

        GridView mGridView = findViewById(R.id.grid); 
    Adapter customAdapter = new Adapter(getApplicationContext(), logos);
    mGridView.setAdapter(customAdapter);
    mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getApplicationContext(),position,Toast.LENGTH_LONG).show();
        }
    });

通常使用回收站视图,但需要列,因此切换到网格视图。我找到了使图像高度相等的来源。但我不想要那个。我不希望它保持纵横比,而只是填满所有可用空间

【问题讨论】:

  • 设置 imageview 的高度为 50 或 100dp
  • @PrajwalW 我希望根据图片的高度调整高度,而不是为所有图片设置高度
  • 如果是这种情况,根据您的要求,您为您的代码获得的输出是正确的......

标签: java android xml android-studio gridview


【解决方案1】:

解决方案:

<?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:padding="1dp"
android:orientation="vertical">

<ImageView
    android:id="@+id/icon"
    android:scaleType="center"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:layout_gravity="center_horizontal"
    android:src="@drawable/logo" />
</LinearLayout>

【讨论】:

  • 仍然无法正常工作。而且我不认为没有 weightsum 的重量是有用的。不过不确定。还有其他解决方案吗?