在android中将多个图像添加到水平滚动视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21089384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding multiple images to horizontal scroll view in android
提问by Amit Choudhry
I am building an application in which I want to add multiple images in a horizontal scrollview and want to scroll these imageson click of two (left and right) srcolling buttons, but I am not able to do that as I am new to android development.So can anybody provide my with the solution how to do it ? Thanks in advance....
我正在构建一个应用程序,在该应用程序中,我想在水平滚动视图中添加多个图像,并希望通过单击两个(左右)srcolling 按钮来滚动这些图像,但由于我是 android 开发的新手,我无法做到这一点。那么有人可以为我提供解决方案吗?提前致谢....
回答by dipali
put this code in your xml
将此代码放在您的xml中
<HorizontalScrollView
android:id="@+id/xml_full_img_hor_below_view"
android:layout_width="match_parent"
android:layout_height="@dimen/full_img_below_relative_height"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/xml_full_img_linear_below_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
///////////////////////////////////// java code
///////////////////////////////////// java代码
this model_ProductMaster.listProductImages.size()is images list:
这个model_ProductMaster.listProductImages.size()是图像列表:
private void setProductImageInView() {
LayoutInflater layoutInflater = (LayoutInflater) viewFullImage
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
linear_below_img_view.removeAllViews();
// Set the image height,widt,scaling and margin of the imageview
for (int i = 0; i < model_ProductMaster.listProductImages.size(); i++) {
convertView = layoutInflater.inflate(
R.layout.xml_row_full_image_view, null, false);
final ImageView img = (ImageView) convertView
.findViewById(R.id.xml_full_img_below_middleimg);
img.setMinimumWidth((int) viewFullImage.getResources()
.getDimension(R.dimen.full_img_below_img_width_height));
img.setMaxWidth((int) viewFullImage.getResources().getDimension(
R.dimen.full_img_below_img_width_height));
img.setMinimumHeight((int) viewFullImage.getResources()
.getDimension(R.dimen.full_img_below_img_width_height));
img.setMaxHeight((int) viewFullImage.getResources().getDimension(
R.dimen.full_img_below_img_width_height));
img.setScaleType(ScaleType.CENTER_INSIDE);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10, 0, 0, 0);
layoutParams.gravity = Gravity.CENTER;
convertView.setLayoutParams(layoutParams);
img.setScaleType(ScaleType.FIT_XY);
imageLoader.displayImage(
model_ProductMaster.listProductImages.get(i).ImageUrl, img,
options, animateFirstListener);
linear_below_img_view.addView(convertView);
convertView.setId(i);
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < linear_below_img_view.getChildCount(); i++) {
View view = linear_below_img_view.getChildAt(i);
if (view == v) {
view.setBackgroundResource(R.drawable.xml_popup_border_with_bg);
} else {
view.setBackgroundResource(R.drawable.common_bg_with_dark_border);
}
}
loadImageonCenter(model_ProductMaster.listProductImages
.get(v.getId()).ImageUrl);
System.gc();
}
});
}
linear_below_img_view.getChildAt(0).setBackgroundResource(
R.drawable.xml_popup_border_with_bg);
// Image Starting load
System.gc();
}
回答by dipali
main.xml
主文件
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="100dp" >
<ImageView
android:id="@+id/xml_home_screen_img_leftarrow"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:padding="5dp"
android:src="@drawable/leftarrow" />
<HorizontalScrollView
android:id="@+id/xml_full_img_hor_below_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/xml_full_img_linear_below_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:orientation="horizontal" >
<include
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/xml_row_full_image_view" />
<include
android:layout_width="wrap_content"
android:layout_height="match_parent"
layout="@layout/xml_row_full_image_view" />
<include
android:layout_width="wrap_content"
android:layout_height="match_parent"
layout="@layout/xml_row_full_image_view" />
<include
android:layout_width="wrap_content"
android:layout_height="match_parent"
layout="@layout/xml_row_full_image_view" />
<include
android:layout_width="wrap_content"
android:layout_height="match_parent"
layout="@layout/xml_row_full_image_view" />
</LinearLayout>
</HorizontalScrollView>
<ImageView
android:id="@+id/xml_home_screen_img_rightarrow"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:padding="5dp"
android:src="@drawable/rightarrow" />
</RelativeLayout>
xml_row_full_image_view.xml
xml_row_full_image_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/xml_full_img_below_mainlinear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:orientation="horizontal"
android:padding="4dp" >
<ImageView
android:id="@+id/xml_full_img_below_middleimg"
android:layout_width="50dp"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:src="@drawable/ic_launcher" />
</LinearLayout>