Android - GridLayout 中的方形 ImageButton

标签 android

我是 Android 新手,请耐心等待:)

在我的主屏幕/主 Activity 中,我想要有 2 x N 的网格,所以我想要有两列和 N 行。网格中的项目是 ImageButton。

一切工作正常,当 ImageButtons 中的 img 是 ic_launcher -> 那么网格中的项目就很好。但是当我放置图形(500x500px)时,“图 block ”比屏幕大,我不知道如何“缩放”此图像以适合图 block ,因此它们可以具有与具有 ic_launcher 时相同的尺寸。

这里是代码:

<GridLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="2"
    android:rowCount="3">

    <ImageButton
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher"
        android:scaleType="centerCrop"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_width="wrap_content"
        android:padding="1dp"
        android:elevation="0dp"
        android:layout_gravity="fill" />
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher"
        android:layout_gravity="fill"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"/>      

</GridLayout>

这是一切正常时的屏幕:

enter image description here

我可以做什么来缩放我的图片以适应这个尺寸?

重要的事情:我的图片是正方形比例,所以我想将此图片缩放到正方形大小。

编辑:当我使用我的 img 时,它看起来像这样:

enter image description here

最佳答案

这是我的尝试..

MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private GridView gridView;
    ArrayList<Integer> integerRes = new ArrayList<>();
    {
        integerRes.add(R.drawable.bbent);
        integerRes.add(R.drawable.bbent);
        integerRes.add(R.drawable.bbent);
        integerRes.add(R.drawable.bbent);
        integerRes.add(R.drawable.bbent);
        integerRes.add(R.drawable.bbent);
        integerRes.add(R.drawable.bbent);
        integerRes.add(R.drawable.bbent);
        integerRes.add(R.drawable.bbent);
        integerRes.add(R.drawable.bbent);
        integerRes.add(R.drawable.bbent);
        integerRes.add(R.drawable.bbent);
        integerRes.add(R.drawable.bbent);
        integerRes.add(R.drawable.bbent);
        integerRes.add(R.drawable.bbent);


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gridView = (GridView) findViewById(R.id.grid);
        gridView.setAdapter(new GridAdapter());


    }

    class GridAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return integerRes.size();
        }

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

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

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ImageView imageView=new ImageView(getApplicationContext());

            imageView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,440));
            // for better result in various devices calculate width at runtime and measure height accordingly

            imageView.setImageResource(integerRes.get(i));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            return imageView;
        }
    }

}

网格.xml

<?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:orientation="vertical">

    <GridView
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="2"/>
</LinearLayout>

enter image description here

关于Android - GridLayout 中的方形 ImageButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42318761/

相关文章:

java - Android:在 ScrollView 中膨胀线性 View

android - 自定义 ListView 适配器中的 AlertDialog.Builder 未显示

java - Android SQLite 查询某月某天

android - 如何使对话框的宽度匹配屏幕宽度

java - `static` 和 `non-static` 字段在 Java Android 中获得更好的性能

android - 为什么android中有两个数字

android - 在包裹中查找 Binder 对象

android - 标准和自定义 GATT 特性

Android - 将 XML 解析为 ArrayList

android - 第一个 Android 应用程序 - 如何访问指南针