Android控件之HorizontalScrollView 去掉滚动条 用按钮控制滚动方向

           android.widget.HorizontalScrollView,译为"横向滚动条"!

           

在默认情况下,HorizontalScrollView控件里面的内容在滚动的情况下,会出现滚动条,为了去掉滚动条,只需要在<HorizontalScrollView/>里面加一句 android:scrollbars="none"。

如果想实现在代码里面,点击左(右)按钮【btnLeft(btnRight)】,滚动条里面的内容会向左向右滚动【horizontalScrollViewMM】。代码如下:

滚动条向左滚动:

btnLeft.setOnClickListener( new View.onClickListener(){

horizontalScrollViewMM.arrowScroll(View.FOCUS_LEFT);

});

滚动条向右滚动:

btnRight.setOnClickListener( new View.onClickListener(){

horizontalScrollViewMM.arrowScroll(View.FOCUS_RIGHT);

});


其中:arrowScroll方法:

public boolean arrowScroll (int direction)

响应点击左右箭头时对滚动条的处理。

参数

direction The direction corresponding to the arrow key that was pressed箭头按键所表示的方向

返回值

若此事件成功完成,则返回true;否则返回false。


HorizontalScrollView还有个方法你可能用得上:

public final void smoothScrollBy (int dx, int dy)

类似scrollBy(int, int),但是呈现平滑滚动,而非瞬间滚动(译者注:瞬间滚动——指不显示滚动过程,直接显示滚动后达到的位置)。

参数

dx 要滚动的X轴像素差值(译者注:横向像素差值)

dy 要滚动的Y轴像素差值(译者注:纵向像素差值)



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要修改 Android HorizontalScrollView 内动态添加控件时的宽度以适应滚动条的宽度,可以使用以下代码: ```java HorizontalScrollView scrollView = findViewById(R.id.horizontal_scroll_view); LinearLayout linearLayout = findViewById(R.id.linear_layout); // 添加控件 Button button = new Button(this); linearLayout.addView(button); // 设置控件宽度 int width = 200; // 设置控件宽度为200像素 int height = LinearLayout.LayoutParams.MATCH_PARENT; LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, height); button.setLayoutParams(layoutParams); ``` 在上面的代码中,我们首先获取了 HorizontalScrollView 和 LinearLayout 的实例。然后我们向 LinearLayout 中动态添加了一个 Button 控件。接着,我们使用 LinearLayout.LayoutParams 来设置控件的宽度和高度。在这里,我们将控件的宽度设置为 200 像素,并将高度设置为 MATCH_PARENT,以填充 LinearLayout 的高度。 如果你希望控件的宽度与滚动条的宽度相同,可以使用以下代码来获取 HorizontalScrollView 的宽度,并将控件的宽度设置为相同的值: ```java HorizontalScrollView scrollView = findViewById(R.id.horizontal_scroll_view); LinearLayout linearLayout = findViewById(R.id.linear_layout); // 添加控件 Button button = new Button(this); linearLayout.addView(button); // 设置控件宽度 int width = scrollView.getWidth(); // 设置控件宽度为 ScrollView 的宽度 int height = LinearLayout.LayoutParams.MATCH_PARENT; LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, height); button.setLayoutParams(layoutParams); ``` 在这里,我们使用 getWidth() 方法获取 HorizontalScrollView 的宽度,并将控件的宽度设置为相同的值。这样,控件的宽度就与滚动条的宽度相同了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值