2.2 ScrollView实现上下文滚动
要点:scrollView中的内容一定要比手机尺寸容量大,而且scrollView中只能有一个子元素,通常情况下默认是linearLayout.
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

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

<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!--中间可以加些ImageView-->
</LinearLayout>
</ScrollView>
</LinearLayout>
以上布局文件实现的只是静态的,通常中动态加载布局更加灵活。
布局文件中仅需一个scrollView就可以,java文件中的代码为:
package com.example.scrollview;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;

public class MainActivity extends Activity {
private ScrollView scrollVeiw;
private LinearLayout layout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollVeiw = (ScrollView) this.findViewById(R.id.scrollView1);
layout = (LinearLayout) this.findViewById(R.id.LinearLayout1);
// 用for循环将Images加载进来
for (int i = 1; i < 7; i++) {
ImageView imageView = new ImageView(this);
// 动态加载图片
Drawable drawable = getResources().getDrawable(R.drawable.e1);
// 将图片设置在ImageView中
imageView.setImageDrawable(drawable);
layout.addView(imageView, i);

}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

更多相关文章

  1. Android(安卓)libmp3lame详解
  2. android webview 加载图片过大
  3. 如何从一个activity启动另外一个activity Android(安卓)Eclipse
  4. Android使用wheelView实现简单类似ios PickerView选择器效果
  5. Android(安卓)国际化和屏幕适配
  6. Android——使用TabLayout+ViewPager实现顶部导航栏
  7. apk文件反编译
  8. adb相关问题
  9. React Native for Android(安卓)环境配置

随机推荐

  1. zxing项目源码解读(2.3.0版本,Android部分)
  2. Android Service小实践
  3. Android使用Intent实现页面跳转
  4. Java和Javascript互调的例子 ---------(An
  5. 如何让Android屏幕只能上下翻转
  6. Android:控件GridView的使用实例
  7. android获取gps坐标
  8. android studio3.5.2打包,解决建议使用 "k
  9. [Android Pro] Android fastboot刷机和获
  10. android Theme使用四