【问题标题】:Unwanted GridView spacing in AndroidAndroid中不需要的GridView间距
【发布时间】:2015-07-09 09:14:52
【问题描述】:

我在将 GridView 放在一起时遇到了 Android 中的间距问题。本质上,我正在组装一个 9x9 游戏板。我有一个 GridView 工作正常,但是在水平方向上的每个定义正方形后都有不需要的间距(垂直是可以的) - 所以它不适合工作区域。

编辑:似乎 GridView 已为每平方宽度分配 50dp,而我只希望它为 30dp...不知道为什么或如何更改它?

自定义网格代码:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class CustomGrid extends BaseAdapter {
    private Context mContext;
    private final int[] Imageid;


    public CustomGrid(Context c, int[] Imageid) {
        mContext = c;
        this.Imageid = Imageid;
    }

    @Override
    public int getCount() {
        return Imageid.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View grid;
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            grid = new View(mContext);
            grid = inflater.inflate(R.layout.gridsingle, null);
            ImageView imageView = (ImageView) grid.findViewById(R.id.grid_image);
            imageView.setPadding(0,0,0,0);
            imageView.setAdjustViewBounds(true);
            imageView.setImageResource(Imageid[position]);
        } else {
            grid = (View) convertView;
        }

        return grid;
    }
}

主视图摘录:

public class Gameplay extends Activity {
GridView grid;

    int[] imageId = {
            R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square,
            R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square,
            R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square,
            R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square,
            R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square,
            R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square,
            R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square,
            R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square,
            R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square,R.drawable.square

    };
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gameplay);
 CustomGrid adapter = new CustomGrid(Gameplay.this, imageId);
        grid=(GridView)findViewById(R.id.grid);
            grid.setAdapter(adapter);
            grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(Gameplay.this, "You Clicked x", Toast.LENGTH_SHORT).show();

            }
        });

gridview xml:

   <LinearLayout 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=".MainActivity" >

        <GridView
            android:numColumns="9"
            android:gravity="center"
            android:columnWidth="30dp"
            android:stretchMode="none"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:horizontalSpacing="0dp"
            android:id="@+id/grid"
            />

    </LinearLayout>

图像视图 xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:padding="0dp" >


<ImageView
        android:id="@+id/grid_image"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:stretchMode="none">
    </ImageView>

</LinearLayout>

最终结果是这样的:

我已经尝试了一些显而易见的想法,比如调整宽度和使用 StretchMode 参数,但还没有运气。有什么想法吗?

非常感谢。

【问题讨论】:

  • 尝试设置网格视图的宽度来包裹内容
  • 很遗憾这没有用。只是为了补充我的问题,当我只希望它为 30dp 时,似乎 GridView 已为每平方分配 50dp 宽度......

标签: android gridview


【解决方案1】:

我解决了我的问题 - 原来我的 gridview(id 网格)的布局有冲突 - 一旦我删除了废弃的布局,一切都开始工作了!

【讨论】:

    猜你喜欢
    • 2015-09-30
    • 2012-07-21
    • 2014-12-20
    • 2014-07-02
    • 1970-01-01
    • 1970-01-01
    • 2017-03-20
    • 2020-04-22
    • 2021-09-29
    相关资源
    最近更新 更多