标签:新建
创建的方法:
1.XML中定义数组(在res->arrays新建XML
第一步:<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="books">
<item>今日事今日必</item>
<item>从现在起航</item>
<item>世上无小事</item>
</string-array>
</resources>
第二步:在主XML中指定文件
<ListView
android:id="@+id/list1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/books"
android:divider="#ff0000"
android:dividerHeight="2px"/>
2.1通过ArrayAdapter实现
第一步:新建XML
<?xml version="1.0" encoding="UTF-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="24sp"
android:padding="10px"
android:shadowColor="#00f"(阴影字体颜色)
android:shadowDx="10"(阴影字体偏移的X坐标)
android:shadowDy="10"(阴影字体偏移的Y坐标)
android:shadowRadius="1">(阴影字体的扩大范围,为1显示正常,大于1时显示模糊)
</TextView>
第二步:layout界面XML中增加ListView
第三步:java程序中增加
ListView list2 = (ListView)findViewById(R.id.list2);
String arrList2[]={"字符床","徐国强","生日快乐"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.arraylist, arrList2);
list2.setAdapter(adapter);
2.2,修改2.1中XML代码
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24dp"
android:checkMark="@drawable/ok"(增加的图片)
android:shadowColor="#f0f"
android:shadowDx="4"
android:shadowDy="4"
android:shadowRadius="2"/>
3.继承ListActivity实现
不需要:setContentView(R.layout.activity_main);
添加:
String arrList2[]={"字符床","徐国强","生日快乐"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, arrList2);
setListAdapter(adapter);
4.通过simpleAdapter实现(可以实现复杂的listView)
第一步:主XML中添加
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="#ff0000"
android:dividerHeight="2px"
/>
第二步:新建XML中增加的是(每一行中要显示的内容)
<LinearLayout 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"
tools:context="com.example.xgwlistview.MainActivity" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_parent"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#f00"
android:paddingLeft="10dp"/>
<TextView
android:id="@+id/attr"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="15dp"/>
</LinearLayout>
</LinearLayout>
第三步:java中增加代码:
String[] name={"李清照","徐国强","周杰伦","高虎"};
String[] attrname={"他是一代诗人","会成功","歌手","扮演青面兽杨志"};
int image[] = {R.drawable.libai,R.drawable.nongyu,R.drawable.qingzhao,
R.drawable.tiger};
List<Map<String,Object>> listItem = new ArrayList<Map<String,Object>>();
for(int i=0;i<name.length;i++){
Map<String,Object> item = new HashMap<String,Object>();
item.put("Image",image[i]);
item.put("name",name[i]);
item.put("attrname", attrname[i]);
listItem.add(item);
}
SimpleAdapter adapter = new SimpleAdapter(this, listItem, R.layout.listview,
new String[]{"Image","name","attrname"},
new int[]{R.id.image,R.id.name,R.id.attr});
ListView list = (ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener();
list.setOnItemSelectedListener();
5.通过继承BaseAdapter实现功能
BaseAdapter base = new BaseAdapter()
{
@Override
public int getCount() {
// TODO Auto-generated method stub
return 10;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LinearLayout line = new LinearLayout(MainActivity.this);
line.setOrientation(0);
ImageView image1 = new ImageView(MainActivity.this);
image1.setImageResource(R.drawable.libai);
TextView text = new TextView(MainActivity.this);
text.setText("第"+(position+1)+"行");
text.setTextColor(Color.BLUE);
text.setTextSize(20);
line.addView(image1);
line.addView(text);
return line;
}
};
mylist.setAdapter(base);
6.自动填充单元格
第一步:XML中定义
<AutoCompleteTextView
android:id="@+id/auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:completionHint="请选择你喜欢的人物"
android:dropDownHorizontalOffset="10dp"
android:completionThreshold="2"/>(数字代表多少个字就会有提示)
<MultiAutoCompleteTextView
android:id="@+id/multiauto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:completionThreshold="2"/>
第二步:java中定义
AutoCompleteTextView auto;
MultiAutoCompleteTextView multi;
ArrayAdapter<String> aa= new ArrayAdapter<String(this,android.R.layout.simple_dropdown_item_1line,name);
auto = (AutoCompleteTextView)findViewById(R.id.auto);
auto.setAdapter(aa);
multi = (MultiAutoCompleteTextView)findViewById(R.id.multiauto);
multi.setAdapter(aa);
multi.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
标签:新建
原文地址:http://1439491420.blog.51cto.com/3736621/1581684