传送门 ☞ Android兵器谱 ☞ 转载请注明 ☞ http://blog.youkuaiyun.com/leverage_1229
君子剑
窈窕淑女,君子好逑。英雄美人,君子淑女,郎才女貌,珠联璧合。梁思成、林徽因结婚,有人赠联:梁上君子,林下美人。月明林下美人来。
今天我们学习如何利用Android平台“君子剑”ScrollView实现垂直滚动浏览信息的功能,ScrollView使用起来非常简单,和HorizontalScrollView正好是一对“冤家”。下面给出该情景的案例:
一、案例技术要点
1.ScorllView控件中摆放一个LinearLayout。不能嵌套摆放其他支持滚动的控件。
2.设置LinearLayout按照垂直方向布局
android:orientation="vertical"
3.LinearLayout布局中所有控件的总高度必须大于屏幕高度。
二、案例代码陈列
工程包目录
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.lynn.scrollview"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:name=".ScrollViewMainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
strings.xml
<resources>
<string name="app_name">Android垂直滚动ScrollView</string>
<string name="view">垂直滚动视图</string>
<string name="more">显示更多</string>
</resources>
main.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/view"
android:textSize="24sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/item1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/more"
android:textSize="24sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/item2" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/item3" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/item4" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/item5" />
</LinearLayout>
</ScrollView>
ScrollViewMainActivity.java
package cn.lynn.scrollview;
import android.app.Activity;
import android.os.Bundle;
/**
* ScrollView支持垂直滚动,并且在ScrollView中只能放置一个控件,通常是一个LinearLayout。
* 另外,该LinearLayout必须采用垂直布局。当LinearLayout中摆放的控件所占用的总高度大于屏幕高度时,
* 就会在屏幕右侧出现一个滚动条。
* ScrollView案例:垂直滚动浏览信息
* @author lynnli1229
*/
public class ScrollViewMainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
三、案例效果展示