android组件之ScrollView


    由于屏幕的高度有限,面对组件显示多组信息时,我们就可以使用scrollview有效的安排组件,浏览时通过滚屏显示,scrollview是FrameLayout 的子类,所以只是提供一个显示的容器。

   使用过程中要注意以下两点 1.scrollview只是提供一个显示的容器,可以包含多个组件进行滚屏;2.scrollview只能包含一种组件,下面通过实例讲解:

1.scrollview的视图定义格式,必须在Scrollview中内嵌一个线性布局,组件的添加通过activity添加

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

    <LinearLayout
        android:id="@+id/mylayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity" >
    </LinearLayout>

</ScrollView>

2.通过activity添加组件

public class MainActivity extends Activity {
	private String[] data= new String[20];

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.activity_main); // 调用默认的布局管理器
		LinearLayout layout = (LinearLayout) super.findViewById(R.id.mylayout); // 取得组件
		LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT); // 定义布局参数
		for (int x = 0; x < this.data.length; x++) { // 通过循环方式将以上的信息通过Button组件进行封装
			
			data[x]="item"+x;
			Button but = new Button(this);
			but.setText(this.data[x]); // 设置显示文字
			layout.addView(but, param); // 增加组件
		}
	}
}

效果图如下:









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值