Scrollview

01)去除滚动条
android:scrollbars=”none”

02)去除阴影部分
在xml中添加:android:fadingEdge=”none”
代码中添加:ScrollView.setHorizontalFadingEdgeEnabled(false);

03)删除ScrollView拉到尽头(顶部、底部),然后继续拉出现的阴影效果
适用于2.3及以上版本
android:overScrollMode=”never”
PS:如果仅设置2,则仍可看到阴影
android:overScrollMode=”never”
所述方法同样适用于去除 GridView ListView ScrollView ViewPager 的蓝色阴影效果;
java 代码去除蓝色阴影效果
调用控件 的 setOverScrollMode 方法即可去除

例如:去除 ScrollView 的蓝色阴影效果 scrollView.setOverScrollMode(ScrollView.OVER_SCROLL_NEVER);
04) 平缓滑动
mScrollView.smoothScrollTo(0,20);
05)在ScrollView嵌套ListView的时候,此方法能够使ListView自适应高度

/*在ScrollView嵌套ListView的时候,此方法能够使ListView自适应高度*/
public static void setListViewHeightBasedOnChildren(ListView listView) {
    // 获取ListView对应的Adapter
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return;
    }
    int totalHeight = 0;
    for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
        // listAdapter.getCount()返回数据项的数目
        View listItem = listAdapter.getView(i, null, listView);
        // 计算子项View 的宽高
        listItem.measure(0, 0);
        // 统计所有子项的总高度
        totalHeight += listItem.getMeasuredHeight();
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    // listView.getDividerHeight()获取子项间分隔符占用的高度
    // params.height最后得到整个ListView完整显示需要的高度
    listView.setLayoutParams(params);
}

06)修改scrollview的背景和颜色
常用属性

android:scrollbars
设置滚动条显示,none(隐藏),horizontal(水平),vertical(垂直)。

android:scrollbarFadeDuration
设置滚动条淡出效果(从有到慢慢的变淡直至消失)时间,以毫秒为单位。Android2.2中滚动条滚动完之后会消失,再滚动又会出来,在1.5、1.6版本里面会一直显示着。

android:scrollbarSize
设置滚动条的宽度。

android:scrollbarStyle
设置滚动条的风格和位置。设置值:insideOverlayinsideInsetoutsideOverlayoutsideInset

android:scrollbarThumbHorizontal
设置水平滚动条的drawableandroid:scrollbarThumbVertical
设置垂直滚动条的drawable.

android:scrollbarTrackHorizontal
设置水平滚动条背景(轨迹)的色drawable

android:soundEffectsEnabled
设置点击或触摸时是否有声音效果

如下为修改垂直scrollview的滚动条的背景和宽度的实例:

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fadingEdge="vertical"
    android:scrollbars="vertical"
    android:scrollbarThumbVertical="@drawable/scroll_bar_bg"
    android:scrollbarSize="6dip"
    android:background="#fefefe">

其中,drawable/scroll_bar_bg.xml文件的内容如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- 填充的颜色:这里设置背景透明 -->
    <solid android:color="#e5e5e5" />
    <!-- 边框的颜色 :不能和窗口背景色一样-->
    <stroke
        android:width="2dp"
        android:color="@android:color/transparent" />
    <!-- 设置按钮的四个角为弧形 -->
    <!-- android:radius 弧形的半径 -->
    <corners android:radius="6dip" />
</shape>

7)内容高度自适应
一般scrollview内嵌一个组件,如一个linearlayout,内部高度设置为wrap_content,scrollview的高度设置为match_parent,如下:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"/>
</ScrollView>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值