【问题标题】:Creating an android layout of image buttons that is scrollable?创建可滚动的图像按钮的android布局?
【发布时间】:2016-08-01 15:40:01
【问题描述】:

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

【问题讨论】:

  • 每张图片的尺寸是多少?你有什么样的例外?
  • 使用带有自定义适配器的 RecyclerView

标签: android listview button layout scrollview


【解决方案1】:

您需要的是网格视图。
供参考
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));

适配器类将视图膨胀到您的 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.developer 站点.. 它会定期从 google 本身获取更新。同样,我在我的答案中包含了一个示例代码。
【解决方案2】:

我建议你使用非对称网格视图,

<com.felipecsl.asymmetricgridview.library.widget.AsymmetricGridView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

更多信息请见Asymmetric Gridview

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多