如何在gridview中制作非方形宽高比按钮?

时间:2016-11-10 00:06:35

标签: android android-gridview

我试图弄清楚如何使用图像适配器制作矩形按钮网格。有没有办法改变高度,使其小于宽度(制作水平矩形)?我尝试更改填充,并编辑xml文件,没有运气。谢谢!

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return MainActivity.SIZE;
    }

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

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

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        //ImageView imageView;
        TextView textView;
        if (convertView == null) {
            // if it's not recycled, initialize some attributes
            /*imageView = new ImageView(mContext);
            imageView.setAdjustViewBounds(true);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(16, 16, 16, 16);*/

            textView = new TextView(mContext);
            textView.setPadding(16,16,16,16);
            textView.setGravity(Gravity.CENTER);
            textView.setText("Button");
            textView.setTextSize(25);
            textView.setTextColor(Color.WHITE);
        } else {
            //imageView = (ImageView) convertView;
            textView = (TextView) convertView;
        }

        //imageView.setImageResource(mThumbIds[position]);
        textView.setBackgroundResource(R.drawable.image);

        //return imageView;
        return textView;
    }

}

1 个答案:

答案 0 :(得分:0)

在android文档中进行了一些搜索,找到了layoutParams方法!

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(300, 175);
textView.setLayoutParams(layoutParams);