Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 删除Gridview中多余的空间_Java_Android_Xml_Android Studio_Gridview - Fatal编程技术网

Java 删除Gridview中多余的空间

Java 删除Gridview中多余的空间,java,android,xml,android-studio,gridview,Java,Android,Xml,Android Studio,Gridview,我希望第三张图片占据空白。当图像的高度相等时,gridview看起来不错,但当其不相等时,它显示为空白,以与下一个图像的高度相匹配。我只想下面的图片来填补空间 适配器 public class Adapter extends BaseAdapter { Context context; int logos[]; LayoutInflater inflter; public Adapter(Context applicationContext, int[] logos) { this.co

我希望第三张图片占据空白。当图像的高度相等时,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


爪哇

GridView mGridView=findviewbyd(R.id.grid);
Adapter customAdapter=新适配器(getApplicationContext(),徽标);
mGridView.setAdapter(customAdapter);
mGridView.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
Toast.makeText(getApplicationContext(),position,Toast.LENGTH_LONG.show();
}
});

通常使用recycler视图,但需要列,因此切换到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>

解决方案:

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


将imageview的高度设置为50或100dp@PrajwalW我希望根据图片的高度来调整高度,而不是为所有图片设置高度。如果是这种情况,则代码的输出根据您的要求是正确的……将imageview的高度设置为50或更高100dp@PrajwalW我想调整一下高度根据图片的高度,而不是为所有图片设置一个高度。如果是这种情况,则代码的输出根据您的要求是正确的……仍然无法正常工作。我不认为没有重量和的重量是有用的。不过我不确定。还有别的办法吗?还是不行,伙计。我不认为没有重量和的重量是有用的。不过我不确定。还有其他解决办法吗?
<?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>