HorizontalScrollView 实现图片横向滚动


功能是这样的,一个 Horizo​​ntalScrollView,里面装了一个自定义的图表DrawChartsView,图表可以左右滑动查看内容

XML文件
<! - 设置图表可以横向滑动 - >
 < Horizo​​ntalScrollView
 android :layout_width = “match_parent”
 android :layout_height = “wrap_content”
 android :id = “@ + id / scrollView” >
            
< LinearLayout
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 HorizontalScrollView 和 LinearLayout 实现横向滚动图片的效果。具体实现步骤如下: 1. 在布局文件中,使用 HorizontalScrollView 包裹 LinearLayout,并设置 LinearLayout 的 orientation 属性为 horizontal,如下所示: ```xml <HorizontalScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/ll_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <!-- 在这里添加图片 --> </LinearLayout> </HorizontalScrollView> ``` 2. 在代码中,获取 LinearLayout 并动态添加图片,如下所示: ```java LinearLayout llHorizontal = findViewById(R.id.ll_horizontal); for (int i = 0; i < imageList.size(); i++) { ImageView imageView = new ImageView(this); imageView.setImageResource(imageList.get(i)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( getResources().getDimensionPixelSize(R.dimen.image_width), getResources().getDimensionPixelSize(R.dimen.image_height)); layoutParams.setMargins(0, 0, getResources().getDimensionPixelSize(R.dimen.image_margin), 0); imageView.setLayoutParams(layoutParams); llHorizontal.addView(imageView); } ``` 其中,imageList 是存放图片资源 ID 的列表,R.dimen.image_width、R.dimen.image_height 和 R.dimen.image_margin 是在 dimens.xml 文件中定义的图片宽度、高度和间距。 这样就可以实现横向滚动图片的效果了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值