2015/11/10
1. AutoCompleteTextView (自动补全):
<AutoCompleteTextView
android:id="@+id/autotv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
AutoCompleteTextView autotv =(AutoCompleteTextView)this.findViewById(R.id.autotv);
autotv.setAdapter(new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1,getdata()));
private String[] getdata(){
String []str=new String[]{"1111","121","113","14"};
return str;
}
String []str={"1111","121","113","14"};//这样写也是对的- -
2. ListView(列表):
<ListView
android:id="@+id/spa_chat_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
android:cacheColorHint="#00000000" >
ListView chat_listView = (ListView) this.findViewById(R.id.spa_chat_list);//实例listview
SimpleAdapter sa=new SimpleAdapter(SpaSubject.this, get_chat_listView_Date(), R.layout.spa_chat_listview,new String[]{"spa_chatName","spa_chatContent","spa_chatTime"},new int[]{R.id.spa_chatName,R.id.spa_chatContent,R.id.spa_chatTime});//实例适配器
//R.layout.spa_chat_listview:是list中item中的布局
chat_listView.setAdapter(sa);//为listview设置适配器
chat_listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v, int i,
long l) {
//点击事件
}
});//为listview中的item设置点击事件
//得到chat_listView的数据
private List<Map<String, Object>> get_chat_listView_Date(){
int length = 30;
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (int i = 1; i <= length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("spa_chatName", "spa_chatName"+i);
map.put("spa_chatContent", "spa_chatContent"+i);
map.put("spa_chatTime", "spa_chatTime"+i);
list.add(map);
}
return list;
}
3. GirdView(网格视图):
以下为别的blog转载- - - -
GirdView的一些属性:
android:numColumns=”auto_fit” ——–列数设置为自动
android:columnWidth=”90dp”,———-每列的宽度,也就是Item的宽度
android:stretchMode=”columnWidth”——缩放与列宽大小同步
android:verticalSpacing=”10dp”———-垂直边距
android:horizontalSpacing=”10dp”——-水平边距
<GridView
android:id="@+id/gview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="auto_fit"
android:columnWidth="80dp"
android:stretchMode="columnWidth"
></GridView>
gview = (GridView) findViewById(R.id.gview);
String [] from ={"image","text"};
int [] to = {R.id.image,R.id.text};
sim_adapter = new SimpleAdapter(this, getData(), R.layout.item, from, to);//新建适配器
gview.setAdapter(sim_adapter); //配置适配器
// 图片封装为一个数组
private int[] icon = { R.drawable.address_book, R.drawable.calendar,
R.drawable.camera, R.drawable.clock, R.drawable.games_control,
R.drawable.messenger, R.drawable.ringtone, R.drawable.settings,
R.drawable.speech_balloon, R.drawable.weather, R.drawable.world,
R.drawable.youtube };
private String[] iconName = { "通讯录", "日历", "照相机", "时钟", "游戏", "短信", "铃声",
"设置", "语音", "天气", "浏览器", "视频" };
public List<Map<String, Object>> getData(){
//cion和iconName的长度是相同的,这里任选其一都可以
for(int i=0;i<icon.length;i++){
Map<String, Object> map = new HashMap<String, Object>();
map.put("image", icon[i]);
map.put("text", iconName[i]);
data_list.add(map);
}
return data_list;
}
4. Adapter(适配器):
//上方有一点点资料
//目前使用simpleAdapter可以满足大部分需求?
5. Spinner(下拉列表)
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Spinner spinner =(Spinner) this.findViewById(R.id.spinner);
ArrayAdapter<String> aa=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item , getdata());
spinner.setAdapter(aa);
private String[] getdata(){
String []str={"1111","121","113","14"};
return str;
}
//点击事件
mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
//str是选中的内容,即是1111,121之类的
String str=parent.getItemAtPosition(position).toString();
Toast.makeText(SpinnerActivity.this, "你点击的是:"+str, 2000).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
6. Gallary(画廊):
本来想写代码学习的,发现Gallery好像过时了。所以先不具体学习了。
7. ScrollView(滚动视图):
ScrollView只支持垂直滚动
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="300dp"
android:text="1"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="300dp"
android:text="1"
/>
</LinearLayout>
</ScrollView>
还有horizontalscrollview:水平滚动