android ListActivity顯示圖標(biāo)實(shí)例
首先,定義列表中的每一行,這里不是用xml文件定義,而是用一個(gè)類定義,CheckBox、ImageView、TextView等控件以addView的方法添加。
//apk列表的一行
class item_apk extends LinearLayout{
public CheckBox chk_apk;
public TextView txt_name;
public TextView txt_flag;
public ImageView img_apk;
public item_apk(Context ctx, String item_name, String item_flag, Drawable item_draw)
{
super(ctx);
this.setOrientation(HORIZONTAL);
chk_apk = new CheckBox(ctx);
addView(chk_apk,
new LinearLayout.LayoutParams((int)(MainActivity.wid_scr*0.2),60));
img_apk = new ImageView(ctx);
img_apk.setImageDrawable(item_draw);
addView(img_apk,
new LinearLayout.LayoutParams((int)(MainActivity.wid_scr*0.2),60));
txt_name = new TextView(ctx);
txt_name.setText(item_name);
addView(txt_name,
new LinearLayout.LayoutParams((int)(MainActivity.wid_scr*0.4),60));
txt_flag = new TextView(ctx);
txt_flag.setText(item_flag);
addView(txt_flag,
new LinearLayout.LayoutParams((int)(MainActivity.wid_scr*0.2),60));
}
}
然后,是定義列表,同樣,也是用一個(gè)類來定義,這里的類繼承自BaseAdapter。
// apk列表
class list_apk extends BaseAdapter{
private Context ctx;
private List<item_apk> list_data;
public list_apk(Context context){
ctx = context;
list_data = new ArrayList<item_apk>();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list_data.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list_data.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return list_data.indexOf(arg0);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
item_apk my_item;
if (convertView==null)
{
my_item = new item_apk(ctx,
(String)list_data.get(position).txt_name.getText(),
(String)list_data.get(position).txt_flag.getText(),
list_data.get(position).img_apk.getDrawable());
}
else
{
my_item = (item_apk)convertView;
my_item.txt_name = list_data.get(position).txt_name;
my_item.txt_flag = list_data.get(position).txt_flag;
my_item.img_apk = list_data.get(position).img_apk;
}
return my_item;
}
public void addItem(String txt_name, String txt_flag, Drawable ico_apk)
{
list_data.add(new item_apk(ctx,txt_name,txt_flag,ico_apk));
}
}
最后,是Activity的類,這里的Activity類的onCreate(Bundle savedInstanceState)里面沒有setContentView()方法,取而代之的是setListAdapter()方法。
public class apk extends ListActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
list_apk list_ada = new list_apk(this);
// 包管理器
PackageManager pm = getPackageManager();
//獲取手機(jī)內(nèi)所有應(yīng)用
List<PackageInfo> pi = pm.getInstalledPackages(0);
list_ada.addItem("應(yīng)用名稱",
"是否系統(tǒng)應(yīng)用",
null);
for (int i=0; i<pi.size(); i++){
PackageInfo pii = (PackageInfo) pi.get(i);
String is_sys;
Drawable icon;
if ((pii.applicationInfo.flags & pii.applicationInfo.FLAG_SYSTEM) <= 0)
is_sys = "否";
else
is_sys = "是";
if (pii.applicationInfo.loadIcon(pm)!=null)
icon = (Drawable)pii.applicationInfo.loadIcon(pm);
else
icon = (Drawable) getResources().getDrawable(R.drawable.ic_launcher);
list_ada.addItem(String.valueOf(pii.applicationInfo.loadLabel(pm)),
is_sys,
icon);
}
setListAdapter(list_ada);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
整個(gè)Activity都是由類構(gòu)成,沒有用到一個(gè)xml布局文件。
運(yùn)行效果如下。
- Android應(yīng)用圖標(biāo)在狀態(tài)欄上顯示實(shí)現(xiàn)原理
- Android界面 NotificationManager使用Bitmap做圖標(biāo)
- android如何添加桌面圖標(biāo)和卸載程序后自動(dòng)刪除圖標(biāo)
- android中TabHost的圖標(biāo)(48×48)和文字疊加解決方法
- Android百度地圖實(shí)現(xiàn)搜索和定位及自定義圖標(biāo)繪制并點(diǎn)擊時(shí)彈出泡泡
- android app icon 圖標(biāo)大小尺寸
- Android不使用自定義布局情況下實(shí)現(xiàn)自定義通知欄圖標(biāo)的方法
- java批量采集豌豆莢網(wǎng)站Android應(yīng)用圖標(biāo)和包名
- Android編程實(shí)現(xiàn)圖標(biāo)拖動(dòng)效果的方法
- Android中正確使用字體圖標(biāo)(iconfont)的方法
相關(guān)文章
Android自定義View實(shí)現(xiàn)搜索框(SearchView)功能
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)搜索框SearchView功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11Android中ExpandableListView的用法實(shí)例
這篇文章主要介紹了Android中ExpandableListView的用法,以實(shí)例形式展示了Android中的下拉list控件的用法,需要的朋友可以參考下2014-10-10Android開發(fā)筆記之:用Enum(枚舉類型)取代整數(shù)集的應(yīng)用詳解
本篇文章是對(duì)Android中用Enum(枚舉類型)取代整數(shù)集的應(yīng)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05Android存儲(chǔ)卡讀寫文件與Application數(shù)據(jù)保存的實(shí)現(xiàn)介紹
這篇文章主要介紹了Android在存儲(chǔ)卡上讀寫文件、Application保存數(shù)據(jù)的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-09-09Android 拍照選擇圖片并上傳功能的實(shí)現(xiàn)思路(包含權(quán)限動(dòng)態(tài)獲取)
這篇文章主要介紹了Android 拍照(選擇圖片)并上傳(包含權(quán)限動(dòng)態(tài)獲取),本文分步驟給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12完美解決Android App啟動(dòng)頁有白屏閃過的問題
這篇文章主要介紹了完美解決Android App啟動(dòng)頁有白屏閃過的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08