gpt4 book ai didi

java - 删除 Gridview 中的多余空间

转载 作者:太空宇宙 更新时间:2023-11-04 09:11:28 25 4
gpt4 key购买 nike

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;
}

}

自定义 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();
}
});

通常使用回收器 View ,但需要列,因此切换到 GridView 。我找到了使图像等高的来源。但我不想要那样。我不希望它保持宽高比,而只是填满所有可用空间

最佳答案

解决方案:

<?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>

关于java - 删除 Gridview 中的多余空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59618271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com