Android 水平滚动图片库 [英] Android Horizontal scrolling image gallery
问题描述
我想创建具有水平图片库(一行和多列)的应用程序.首先我尝试使用 gridview,但它只能用作垂直滚动.我可以为此目的使用 ListView
或 GridView
吗?
I'd like to create app with horizontal image gallery (with one row and multiple columns).
First i try to use gridview, but it can be used as vertical scroll only.
Can i use ListView
or GridView
for that purposes?
推荐答案
在 HorizontalScrollView 中创建 LinearLayout,然后动态创建一个 imageView 并将该 imageview 添加到 linearLayout.
create LinearLayout inside HorizontalScrollView,then create an imageView dynamically and add that imageview to linearLayout.
示例代码:
<HorizontalScrollView
android:id="@+id/horizontal_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/linear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
在onCreate()方法中,从xml文件中获取linearLayout的id,并将动态创建的ImageView添加到linearlayout中:
In onCreate() method,get the id of linearLayout from the xml file and add dynamically created ImageView to linearlayout:
LinearLayout layout = (LinearLayout) findViewById(R.id.linear);
for (int i = 0; i < 10; i++) {
ImageView imageView = new ImageView(this);
imageView.setId(i);
imageView.setPadding(2, 2, 2, 2);
imageView.setImageBitmap(BitmapFactory.decodeResource(
getResources(), R.drawable.ic_launcher));
imageView.setScaleType(ScaleType.FIT_XY);
layout.addView(imageView);
}
这篇关于Android 水平滚动图片库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!