以下程式碼會查詢已安裝的Apps,並將結果顯示在ListView元件:
PackageManager pmPack;
pmPack = getPackageManager();
List packinfo = pmPack.getInstalledPackages(PackageManager.GET_ACTIVITIES);
String[] pak = new String[packinfo.size()];
for ( int i=0; i < packinfo.size(); i++) {
PackageInfo p = (PackageInfo) packinfo.get(i);
pak[i] = p.packageName;
}
ListView lv = (ListView) findViewById(R.id.listView1);
ArrayAdapter lstadapter = new ArrayAdapter(
MainActivity.this,
android.R.layout.simple_list_item_1, pak);
lv.setAdapter(lstadapter);
2012年9月28日 星期五
Android程式設計 - 使用ColorStateList
在Android中設定元件顏色值時,有些情況下並不是只有單一顏色,而要與元件狀態關聯;例如按鈕按下時與放開後要使用不同顏色,這種情況就需要使用ColorStateList。
首先定義顏色,例如res/drawable/tab_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected -->
< tem android:state_selected="true" android:color="#000000" />
<!-- When not selected -->
<item android:state_selected="false" android:color="#CFCFCF" />
<!-- When pressed -->
<item android:state_pressed="true" android:color="#FF0000" />
</selector>
程式中可透過以下方式建立ColorStateList,再設定到元件上。
TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);
try {
XmlResourceParser parser = getResources().getXml(R.drawable.tab_color);
ColorStateList colors;
colors = ColorStateList.createFromXml(getResources(), parser);
tv.setTextColor(colors);
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
首先定義顏色,例如res/drawable/tab_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected -->
< tem android:state_selected="true" android:color="#000000" />
<!-- When not selected -->
<item android:state_selected="false" android:color="#CFCFCF" />
<!-- When pressed -->
<item android:state_pressed="true" android:color="#FF0000" />
</selector>
程式中可透過以下方式建立ColorStateList,再設定到元件上。
TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);
try {
XmlResourceParser parser = getResources().getXml(R.drawable.tab_color);
ColorStateList colors;
colors = ColorStateList.createFromXml(getResources(), parser);
tv.setTextColor(colors);
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
2012年9月23日 星期日
Android程式設計 - 使用ExpandableList
ExpandableListView提供分組的項目清單介面,使用者可將每一組的清單內容展開或是折疊起來。
1.
首先要建立ExpandableListView所使用的版面,包括群組版面及群組的子版面。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/text_college"
... />
</RelativeLayout>
child.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/image_go"
... />
<TextView
android:id="@+id/text_department"
... />
</RelativeLayout>
2.
建立ExpandableListView之資料轉接器
public class CollegeListAdapter extends
BaseExpandableListAdapter {
private Context context;
private ArrayList groups;
private ArrayList>
childs;
public CollegeListAdapter(Context
context, ArrayList groups, ArrayList>
childs) {
this.context = context;
this.groups = groups;
this.childs = childs;
}
public Object getChild(int groupPosition, int childPosition) {
return childs.get(groupPosition).get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView,
ViewGroup parent) {
View childView;
@SuppressWarnings("unchecked")
String department = childs.get(groupPosition).get(childPosition);
LayoutInflater
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//建立子清單元件, 並設置元件對應的內容
childView =
layoutInflater.inflate(R.layout.child, null);
TextView dept_name =
(TextView) childView.findViewById(R.id.text_department);
dept_name.setText(department);
ImageView imageView =
(ImageView)childView.findViewById(R.id.image_go);
imageView.setImageResource(R.drawable.ic_go);
return childView;
}
public int getChildrenCount(int groupPosition) {
return childs.get(groupPosition).size();
}
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
public int getGroupCount() {
return groups.size();
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup
parent) {
View groupView;
String college = groups.get(groupPosition);
LayoutInflater
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//建立群組元件,並設置元件對應的內容
groupView =
layoutInflater.inflate(R.layout.group, null);
TextView college_name =
(TextView)groupView.findViewById(R.id.text_college);
college_name.setText(college);
return groupView;
}
public boolean hasStableIds() {
return false;
}
public boolean isChildSelectable(int groupPos, int childPos) {
return false; //如果想要子列表可被點選,要改為回傳true
}
}
3. 建立ExpandableListView所要顯示資料串列。
ArrayList group; //群組列表
ArrayList>
child; //子列表
...
private void initializeData(){
group = new ArrayList();
child = new ArrayList>();
addInfo("電資學院",
new String[]{"電子系","電機系","資訊系","電腦通訊系"});
new String[]{"電子系","電機系","資訊系","電腦通訊系"});
addInfo("工程學院",
new String[]{"機械系","自動化系","工管系"});
new String[]{"機械系","自動化系","工管系"});
}
private void addInfo(String g,String[] c){
group.add(g);
ArrayList
childitem = new ArrayList();
for(int i=0;ilength;i++){
childitem.add(c[i]);
}
child.add(childitem);
}
4. 設定ExpandableListView資料轉接器
expandablelist = (ExpandableListView)
findViewById(R.id.expandableListView1);
expandablelist.setAdapter(
new CollegeListAdapter(MainActivity.this, group, child));
new CollegeListAdapter(MainActivity.this, group, child));
訂閱:
文章 (Atom)