滚动条~~~xml方式(一)

2种方法

1是以xml为主:

View Code
1 <?xml version="1.0" encoding="utf-8"?>
2  <LinearLayout android:orientation="horizontal"
3 xmlns:android="http://schemas.android.com/apk/res/android"
4 android:layout_width="fill_parent" android:layout_height="wrap_content">
5 <TextView android:text="这是滚动" android:textColor="#f7b400"
6 android:background="#603012" android:layout_width="wrap_content"
7 android:layout_height="30dip"></TextView>
8 <ScrollView android:id="@+id/ScrollView01"
9 android:layout_width="wrap_content" android:layout_height="wrap_content"
10 xmlns:android="http://schemas.android.com/apk/res/android">
11 <HorizontalScrollView android:id="@+id/HorizontalScrollView01"
12 android:layout_width="fill_parent" android:layout_height="fill_parent"
13 xmlns:android="http://schemas.android.com/apk/res/android">
14 <TableLayout android:id="@+id/TableLayout01"
15 android:layout_width="fill_parent" android:layout_height="fill_parent"
16 android:stretchColumns="0" android:background="#603012"
17 xmlns:android="http://schemas.android.com/apk/res/android">
18 <TableRow android:layout_width="fill_parent"
19 android:layout_height="30dip">
20 <Button android:text=" I am 001" android:textColor="#f7b400"
21 android:layout_width="fill_parent" android:layout_height="fill_parent">
22 </Button>
23 <Button android:text=" I am 002" android:textColor="#f7b400"
24 android:layout_width="fill_parent" android:layout_height="fill_parent">
25 </Button>
26 <Button android:text=" I am 003" android:textColor="#f7b400"
27 android:layout_width="fill_parent" android:layout_height="fill_parent">
28 </Button>
29 <Button android:text=" I am 004" android:textColor="#f7b400"
30 android:layout_width="fill_parent" android:layout_height="fill_parent">
31 </Button>
32 <Button android:text=" I am 005" android:textColor="#f7b400"
33 android:layout_width="fill_parent" android:layout_height="fill_parent">
34 </Button>
35 <Button android:text=" I am 006" android:textColor="#f7b400"
36 android:layout_width="fill_parent" android:layout_height="fill_parent">
37 </Button>
38 <Button android:text=" I am 007" android:textColor="#f7b400"
39 android:layout_width="fill_parent" android:layout_height="fill_parent">
40 </Button>
41 </TableRow>
42 </TableLayout>
43 </HorizontalScrollView>
44 </ScrollView>
45  </LinearLayout>

最外层是线性布局horizontal横向排列

第二层是2个:一个是textview另一个就是ScrollView(上下滚动)

而在ScrollView里面又套了一层HorizontalScrollView(左右滚动)

在这两层滚动里面的就是TableLayout

TableLayout里面是一个TableRow,在TableRow里面是7个button

基本上的布局完成。

如果我只要左右滚动而不要上下滚动怎么办?

试了下把ScrollView屏蔽只用HorizontalScrollView

结果报错Caused by: java.lang.ClassCastException: android.widget.ScrollView

原因是ScrollView是父控件如果没有它那么HorizontalScrollView是无法使用的。

这是个问题(暂留)

下面看看java代码部分:

View Code
1 package com.hsm.activity;
2
3
4  import android.app.Activity;
5  import android.os.Bundle;
6  import android.widget.HorizontalScrollView;
7  import android.widget.ScrollView;
8
9  publicclass FirstActivity extends Activity
10 {
11 private ScrollView mScrollView;
12 private HorizontalScrollView hScrollView;
13 /** Called when the activity is first created. */
14 @Override
15 publicvoid onCreate(Bundle savedInstanceState)
16 {
17 super.onCreate(savedInstanceState);
18 setContentView(R.layout.main);
19 mScrollView = (ScrollView)findViewById(R.id.ScrollView01);
20 hScrollView = (HorizontalScrollView)findViewById(R.id.HorizontalScrollView01);
21 mScrollView.setVerticalScrollBarEnabled(false);
22 hScrollView.setHorizontalScrollBarEnabled(false);
23
24
25
26 }
27
28
29 }

这个也没有必要废话了~~主要的就是21,22行两行代码这两个的意思就是是否显示滚动条false不显示true显示

如果要在xml文件中设置是否隐藏滚动条

android:scrollbars="none"

由于只做了一行所以显示是这个样子,

如果做多行可以但是是需要手动添加,很明显是很麻烦的。。。。。看看能不能有好方法.

而且这样的布局远远达不到我的要求所以必须要更改哈哈

(未完待续卡卡)


posted @ 2011-06-08 09:39  飞翔的熊猫  阅读(1580)  评论(0编辑  收藏  举报