android - 创建可滚动的图像按钮的 android 布局?

标签 android listview button layout scrollview

我一直在尝试创建类似于图片的图像按钮布局,前提是可以垂直滚动。 Desired Android layout .我正在使用 android studio。如果我添加的图像按钮多于 12 个(只够填满一个屏幕),应用程序就会崩溃。
我希望能够在 3 行中有许多可以向下滚动的图像按钮。我尝试过使用 ScrollViewListView 但没有成功。有没有更好的方法来做到这一点?

最佳答案

你需要的是 GridView 。
供引用
http://developer.android.com/guide/topics/ui/layout/gridview.html

在您的布局中包含 GridView

<?xml version="1.0" encoding="utf-8"?>
    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
/>

在您的 java 类中引用该 GridLayout 并为其设置适配器。

 GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));

适配器类将 View 扩展到您的 GridLayout。

public class ImageAdapter extends BaseAdapter {
private Context mContext;

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

public int getCount() {
    return mThumbIds.length;
}

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;
    if (convertView == null) {
        // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

// references to our images
private Integer[] mThumbIds = {
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7
};
}

关于android - 创建可滚动的图像按钮的 android 布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36549332/

相关文章:

button - 如何获取按钮发件人 ID?在 SwiftUI 中

button - Xamarin.Forms:删除按钮内的填充

Android Facebook 登录和网络服务器

java - 从 UI 线程调用 GattCallback 中的函数

java - 安装 Android ADT 14 : "R cannot be resolved" 后

java - ObservableList.remove(index, index+1) 导致 UnsupportedOperationException 并仍然从列表中删除

android - 从编辑文本中过滤 ListView

java - 如何修复 ListView 中的搜索栏功能?

javascript - 当我单击按钮时,将变量作为文本放在按钮上

android - 如果我删除 gps.conf 文件会发生什么