码迷,mamicode.com
首页 > 移动开发 > 详细

android常用组件之ScrollView

时间:2014-12-27 11:31:16      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

ScrollView在android中可以实现滚动视图,手机屏幕大小有限,当显示的内容较多时,滚动视图就派上用场了。

该实例主要是通过布局文件生成视图,只是演示ScrollView的使用,没有考虑整体美观效果。总体来看,采用垂直布局,在垂直布局的第一行加入一个TextView组件;第二行是一个ScrollView,内部加入垂直布局;第三行再加入一个TextView组件。

在布局文件中使用ScrollView,一般是在ScrollView节点中加入布局方式,如下所示,ScrollView也可以作为根节点。

<ScrollView ... >

   <LinearLayout ...>

       ...

  </ LinearLayout>

</ ScrollView>

首先是布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="main.test_scrollview.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="100sp"
        android:background="#ff0000ff"
        android:textColor="#ffffffff"
        android:text="@string/tv1"
        android:gravity="center"/>
    <ScrollView 
        android:id="@+id/scrollView"
        android:layout_width="wrap_content"
        android:layout_height="200dp">
       <LinearLayout 
           android:id="@+id/linearLayout2"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:orientation="vertical">

           <ProgressBar
               android:id="@+id/progressBar1"
               style="?android:attr/progressBarStyleLarge"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content" />

           <ProgressBar
               android:id="@+id/progressBar2"
               style="?android:attr/progressBarStyleLarge"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content" />
           <View 
               android:id="@+id/view1"
               android:layout_width="wrap_content"
               android:layout_height="100sp"
               android:background="#ff0" />
           <ProgressBar
               android:id="@+id/progressBar3"
               style="?android:attr/progressBarStyleLarge"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content" />
           <ProgressBar
               android:id="@+id/progressBar4"
               style="?android:attr/progressBarStyleLarge"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content" />
           <View 
               android:id="@+id/view2"
               android:layout_width="wrap_content"
               android:layout_height="100sp"
               android:background="#f00" />
           <!-- 自定义了一个View组件显示 -->
           <ProgressBar
               android:id="@+id/progressBar5"
               style="?android:attr/progressBarStyleLarge"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content" />
           <ProgressBar
               android:id="@+id/progressBar6"
               style="?android:attr/progressBarStyleLarge"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content" />
       </LinearLayout> 
    </ScrollView>
    <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="100sp"
        android:background="#ff0000ff"
        android:gravity="center"
        android:textColor="#ffffffff"
        android:text="@string/tv2" />
       
</LinearLayout>

其次是strings.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Test_ScrollView</string>
    <string name="tv1">上边界</string>
    <string name="action_settings">Settings</string>
    <string name="tv2">下边界</string>

</resources>

再次是android源代码文件(只是引用了布局文件,其他没有任何改动):

package main.test_scrollview;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;

public class MainActivity extends ActionBarActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);      
    }
}

最后是测试结果:

技术分享

android常用组件之ScrollView

标签:

原文地址:http://my.oschina.net/u/2243176/blog/361112

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!