在 GridView 中拉伸的按钮

Buttons stretching in GridView

我有一个网格视图和一个自定义适配器来创建可变数量的按钮。都有他们应该保持他们共同背景的比例(正方形)。

但我的问题是我无法让我的网格视图拉伸它们以保持它们的 "ratio" height/width。它们根本不是正方形..

最好的办法是它们保持所有方形并调整大小以填满屏幕。

这是我的适配器代码

 public class ButtonAdapter extends BaseAdapter {
    private Context mContext;


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

    public int getCount() {
        return size*size;
    }

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        Button btn;
        if (convertView == null) {
            btn = new Button(mContext);
            btn.setPadding(2, 2, 2, 2);
        }
        else {
            btn = (Button) convertView;
        }

        btn.setId(position);
        btn.setBackgroundResource(R.drawable.undiscovered);
        btn.setOnClickListener(new MyOnClickListener(position));
        return btn;
    }
}

这是我的 xml

<RelativeLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.gabriel.minesweeper.GameActivity">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:id="@+id/GameTextview"/>

    <GridView
        android:id="@+id/gridview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:stretchMode="columnWidth"
        android:gravity="center"/>


</LinearLayout>

</RelativeLayout>

使用 ImageButton class(扩展 Button),我们可以得到你想要的东西......(滚动到底部查看完整代码示例)

获取 parent View 的宽度,在你的例子中是 GridView

double parentWidth = ((MainActivity) mContext).findViewById(R.id.gridview).getWidth();

获取按钮图像尺寸

double width = button.getDrawable().getIntrinsicWidth();
double height = button.getDrawable().getIntrinsicHeight();

正在计算 X-Axis

中比例缩放的缩放因子

为了比例缩放 scalex= 1scaley想做图片宽度和图片宽度的比例parent View。我们这样计算:

double scalex = parentWidth / parentWidth;
double scaley = parentWidth / width;

使用比例因子计算新维度

int newWidth = (int) (width * scalex);
int newHeight = (int) (height * scaley);

对于 正方形 ImageButton 使用此代码:

int newWidth = (int) parentWidth;
int newHeight = (int) parentWidth;

ImageButton 缩放类型

缩放类型 CENTER_INSIDE 确保图像相对于 ImageButton 居中显示,并且以全尺寸显示而不会失真。

button.setScaleType(ImageButton.ScaleType.CENTER_INSIDE);

这里是全部:

public View getView(int position, View convertView, ViewGroup parent) {
    final ImageButton button;
    if (convertView == null) {
        button = new ImageButton(mContext);
        button.setPadding(2,2,2,2);

    } else {
        button = (ImageButton) convertView;
    }


    button.setId(position);
    button.setImageResource(mThumbIds[position]);
    button.setScaleType(ImageButton.ScaleType.CENTER_INSIDE);

    //Scale button using layout params
    double parentWidth = ((MainActivity) mContext).findViewById(R.id.gridview).getWidth();
    double width = button.getDrawable().getIntrinsicWidth();
    double height = button.getDrawable().getIntrinsicHeight();
    double scalex = parentWidth / parentWidth;
    double scaley = parentWidth / width;
    int newWidth = (int) (width * scalex);
    int newHeight = (int) (height * scaley);

    button.setLayoutParams(new GridView.LayoutParams(newWidth, newHeight));

    return button;
}