注:源自传智播客视频教程
现整理此案例,以供日后自己或大家学习、参考:
自定义组合控件demo
实现效果:
下载链接:
http://download.csdn.net/detail/wang725/8738627
主要代码:
1.MainActivity.java
package com.example.desctop;
import java.util.ArrayList;
import java.util.List;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private List<String> packageNames;
private GridView gv;
private PackageManager pm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv = (GridView) findViewById(R.id.gv);
pm = getPackageManager();
Intent intent = new Intent();
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
// 查询出来手机上所有具有启动能力的activity
List<ResolveInfo> infos = pm.queryIntentActivities(intent, PackageManager.GET_INTENT_FILTERS);
packageNames = new ArrayList<String>();
for(ResolveInfo info : infos) {
String packageName = info.activityInfo.packageName;
packageNames.add(packageName);
}
gv.setAdapter(new MyAdapter());
gv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// pm.queryIntentActivities(intent, flags)
Intent intent = pm.getLaunchIntentForPackage(packageNames.get(position));
if(intent != null) {
startActivity(intent);
} else {
Toast.makeText(MainActivity.this, "soryy,该应用不能启动。", 0).show();
}
}
});
}
private class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return packageNames.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = View.inflate(getApplicationContext(), R.layout.item, null);
ImageView iv = (ImageView) view.findViewById(R.id.iv);
TextView tv = (TextView) view.findViewById(R.id.tv);
String packageName = packageNames.get(position).toString();
try {
iv.setImageDrawable(pm.getPackageInfo(packageName, 0).applicationInfo.loadIcon(pm));
tv.setText(pm.getPackageInfo(packageName, 0).applicationInfo.loadLabel(pm));
} catch (Exception e) {
e.printStackTrace();
}
return view;
}
@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 0;
}
}
/**
* 屏蔽返回键
*/
@Override
public void onBackPressed() {
}
}
2.布局
activity_main.xml
<RelativeLayout 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" >
<GridView
android:numColumns="4"
android:overScrollMode="never"
android:id="@+id/gv"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/iv"
android:layout_width="50dip"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv"
android:singleLine="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"/>
</LinearLayout>