8月12日
今天发现了一个牛逼人的博客之类的东西。http://happycasts.net/
ScorllView滚动视图。由FrameLayout派生而成。
他就是一个用于为普通组件添加滚动条的组建。
ScorllView里只能含有一个组件,而ScorllView的作用就是为该组件添加垂直的滚动条。
只能有一个组件。所以一般我们要让一个界面实现滚动都不能直接用ScrollView来包裹。
而是先用一个LinearLayout来包裹,并且这个LinearLayout的Layout_height都是wrap_content的。
如果需要实现水平竖直都滚动,可以这样做:
<!--定义ScorllView为里面的组件添加垂直滚动条 -->
<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" >
<!-- 定义HorizontalScrollView为里面的组件添加水平滚动条 -->
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
关于listview的使用,如果只是显示简单的数组。
可以直接在xml文件中配置。
首先在values目录下新建一个xml文件。
在新建该xml文件出现的导向中选择root element为resources.
比如如下的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="books">
<item>thinking</item>
<item>hello</item>
<item>microsoft</item>
<item>google</item>
</string-array>
</resources>
然后在布局文件中直接配置该listview.
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/books"
>
</ListView/>
在该listView中entries直接指定引用的数组。
还可以指定listview的分割线相关属性,通过
android:divider=""
android:dividerHeight=""
另外,由于显示的只是一个简单的数组的内容,所以还可以使用ArrayAdapter.
首先定义要显示的数组。
在该ArrayAdapter的构造函数中第一个参数为Context,
第二个参数为该listview中每一个item的布局。
第三个参数为要显示的数组。
该ArrayAdapter在构造时有点泛型的意思。
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Context,布局,要显示的数组);
其中<>里的类型由要显示的数组的类型决定。
而第二个参数,即每一个item的布局,如果较为简单,android提供了几个较简单的实现。
android.R.layout.simple_list_item_1
android.R.layout.simple_list_item_2
android.R.layout.simple_list_item_multiple_choice
android.R.layout.simple_list_item_single_choice
关于在ListView中使用SimpleAdapter 。
首先定义两个数组。一个显示文字的String数组和一个显示图片在drawable文件夹下id的int数组。
private String[] names = {"name1","name2","name3"};
private int[] images = {R.drawable.a1,R.drawable.a2,R.drawable.a3};
然后定义一个List<Map<String,Object>> lists = new ArrayList<Map<String,Object>>();
for(int i = 0; i < names.length;i++)
{
Map<String,Object> item = new HashMap<String,Object>();
item.put("name",names[i]);
item.put("image",images[i]);
lists.add(item);
}
再定义出SimpleAdapter
SimpleAdapter adapter = new SimpleAdapter
(
Context,lists,item的布局文件,
new String[]{"name","image"},
new int[]{R.id.name,R.id.image}
);
这里的第三个参数为listView中每一个item的布局。
第四个参数中String数组中的两个值即为上面的map集合里的两个key.
第五个参int数组中的两个值为listview中的item中的控件的id.
这里的控件id的顺序要跟上面第四个参数的顺序一致。
posted on 2012-08-12 23:35 lightman_21 阅读(119) 评论(0) 收藏 举报