安卓ScrollView总结

转载于:                                                                     

        一.滚动视图的基础概念

       滚动视图用于为其它组件添加滚动条,在默认的情况下,当窗体中内容比较多,而一屏显示不下时, 超出的部分不能被用户所看到.因为Android的布局管理器本身没有提供滚动屏幕的功能.如果 要让其滚动,就要使用滚动视图ScrllView.
       滚动视图是FrameLayout的子类,因此,在滚动视图中,可以添加任何想要放入其中的组件,但是一个滚动视图中只能放一个组件,如果要放置多个,可以先放一个存布局管理器.再将要放置的组件放置到该布局管理器中,在滚动视图中,使用比较多的是线性布局管理器.

(一)滚动视图(ScrollView)的XML配置:

    <ScrollView
android:id="@+id/myscollView"  
android:layout_width="match_parent" 
 android:layout_height="wrap_content" > 
//这里只能放一个布局或控件
//一般是放LinearLayout布局
</ScrollView>

(二)水平滚动视图(HorizontalScrollView)

HorizontalScrollView和ScrollView差不多,只是滚动方式为横向
XML配置:

<HorizontalScrollView 
android:id="@+id/scrollView1" 
android:layout_width="match_parent"
android:layout_height="wrap_content" >
 </HorizontalScrollView>

滚动视图的作用:

       1.滚动视图使用后能让里面的视图控件没展示的部分滚动后可以展示
比如一个TextView默认情况文本过大后,超出屏幕或框体的内容是不能显示出来的,但是如果把这个TextView放到一个滚动视图中,就能上下滚动显示没有显示的文本内容。
       2.水平滚动的视图内显示横向拉伸的布局的内容
比如一个水平的LinearLayout放置十个按钮,那么只能显示五个,如果把这个LinearLayout放在一个水平滚动的视图中,就可以水平的拖动视图显示后面的按钮。

       值得注意的是ListView是默认带滚动的,不需要滚动视图的包裹。
       而Android的五六布局(线性布局,相对布局,绝对布局,表格布局,网格布局,层布局)和简单控件(TextView,ImageView等等)默认是不能滚动显示的,除非放到滚动视图中。
       还有一点值得注意的是,如果ListView放到ScrollView中去这里ListView要自定义的拉长,否则会默认显示ListView中的一行数据,要拉伸才显示其他内容。

二.简单展示的示例程序

(一)布局文件activity_main.xml设计

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollbars="none" >

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

            <TextView
                android:id="@+id/tv_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/tv_content"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </ScrollView>

</RelativeLayout>

(二)java代码设计

package com.example.lesson7_scrollview;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

/**
 * ScrollView的简单展示
 */
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tvTile = (TextView) findViewById(R.id.tv_title);
        tvTile.setTextSize(30);
        tvTile.setText("文章标题:。。。");
        TextView tv = (TextView) findViewById(R.id.tv_content);
        tv.setTextSize(20);
        CharSequence text = "锅盔的原材料都是庄稼人自家酿种的。" 
        + "\n" + "面粉是自家地里种的小麦磨成的;"
        + "\n" + "是自家饲养的鸡产的;香豆子," + "\n"
                + "是自家地埂上撒出来的。这些材料," + "\n" 
                +  "也是靠老天的眷顾,才有的。山区," + "\n"
                + "就是靠天吃饭。万一哪一年," + "\n" 
                + "老天一生气不下雨,山就荒了," + "\n"
                + "地也荒了,人也慌了," + "\n"  
                + "妈妈的锅盔也会瘪了。" + "\n"
                + "妈妈经过泡发面、和面、醒面等几道程序," + "\n" 
                + "把这些材料依次揉进面里," + "\n"
                + "再切成碗口大的面团,揉成馒头,再用擀杖稍稍一擀,"
                + "\n"
                + "或用手掌稍稍一按,就成了一寸多高、盘子大小的圆饼。" 
                + "\n" + "灵巧、细心、唯美的妈妈总不忘在饼上面"
                + "\n" + "用菜刀画出美丽对称的图案:三角形、四边形、菱形等等。" + "\n"
                + "妈妈的爱,就在那揉、擀、按、画的过程中," + "\n" 
                + "一点一点渗进锅盔里,流进我的血脉里。弄好的锅";
        tv.setText(text);

    }

}

程序运行后的显示界面:

g1

页面往下拉显示的界面:
g2

如果ListView或GridView放在ScrollVIew中要做的处理:

如果是ListView在ScrollView中时的处理:

package com.lwz.mathbox.weight;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;

/**
 * 可滚动的ListView,这里是因为这个ListView被包裹在一个ScrollView中才需要设置纵向拉伸
 */

public class ScrollListView extends ListView {
    public ScrollListView(Context context) {
        super(context);
    }

    public ScrollListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int height = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, height);
    }
}

使用的时候把这两个类分别当中ListView和GridView使用就可以了。

比如:

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

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/hint_color"
                android:padding="10dp"
                android:text="历史记录"
                />

            <TextView
                android:id="@+id/tv_history"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:text="当前还没有历史记录" />
            //包名+类名
            <com.lwz.mathbox.weight.ScrollGridView
                android:id="@+id/gv_topic"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/hint_color"
                android:horizontalSpacing="1dp"
                android:numColumns="2"
                android:verticalSpacing="1dp" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/hint_color"
                android:padding="10dp"
                android:text="社区热门"
               />
            //包名+类名
            <com.lwz.mathbox.weight.ScrollListView
                android:id="@+id/lv_topic"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

        </LinearLayout>
    </ScrollView>

转载于: https://blog.csdn.net/wenzhi20102321/article/details/53491176


  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值