編輯:關於Android編程
先看效果圖
這個是我們自己的apk點擊之後的效果
下邊是布局文件<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+YWN0aXZpdHlfbWFpbi54bWzW97K8vtbOxLz+PGJyPgo8L3A+CjxwPjxMaW5lYXJMYXlvdXQgeG1sbnM6YW5kcm9pZD0="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=".MainActivity"
android:orientation="vertical">
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="15dp"
android:text="@string/app_name"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
我用一個GridView 做容器
下邊是單個item布局文件
application_layout.xml
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:gravity="center"/>
下邊是AndroidManifest.xml,這個裡邊有幾個和普通apk不同的地方
android:versionCode="1"
android:versionName="1.0" >
android:targetSdkVersion="17" />
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
android:name="com.wind.lancherdemo.MainActivity"
android:theme="@android:style/Theme.Dialog"
android:label="@string/app_name" >
下邊是源文件
package com.wind.lancherdemo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity implements OnItemClickListener{
private GridView mGridView;
private Context mContext;
private PackageManager mPackageManager;
private List
private List
private static final String[] mShowAppPkgNames = {"com.android.contacts","com.android.mms","com.android.browser"}; //這個地方可以添加我們需要過濾的apk包名
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
setupViews();
}
private void setupViews() {
mContext = MainActivity.this;
mPackageManager = getPackageManager();
mGridView = (GridView)findViewById(R.id.allapps);
bindAllApps();
mGridView.setAdapter(new GridItemAdapter(mContext, mShowApps)); //這個地方時設置GridView的適配器,不懂的可以去網上搜下具體教程
mGridView.setNumColumns(3);
mGridView.setOnItemClickListener(this);
}
private void bindAllApps() {
Intent mainIntent = new Intent(Intent.ACTION_MAIN,null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mAllApps = mPackageManager.queryIntentActivities(mainIntent, 0);//這個地方就是我們根據我們安裝的所有apk過濾出我們想要的apk,這樣做的目的是你刪除了某個我們需要的應用,我們的程序依然正常
for (ResolveInfo app_item : mAllApps) {
String pkg = app_item.activityInfo.packageName;
for (int i = 0; i < mShowAppPkgNames.length; i++) {
if(mShowAppPkgNames[i].equals(pkg)) {
mShowApps.add(app_item);
}
}
}
Collections.sort(mShowApps, new ResolveInfo.DisplayNameComparator(mPackageManager));
}
//這個是根據我們的點擊進入到具體的應用
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
ResolveInfo res = mShowApps.get(position);
String pkg = res.activityInfo.packageName;
String cls = res.activityInfo.name;
ComponentName component = new ComponentName(pkg, cls);
Intent i = new Intent();
i.setComponent(component);
startActivity(i);
}
//這個地方是我們重寫我們的GridView適配器
private class GridItemAdapter extends BaseAdapter{
private Context context;
private List
public GridItemAdapter(Context context, List
this.context = context;
this.resInfo = resInfo;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return resInfo.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return resInfo.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
//這個地方用ViewHolder的目的是不用我們每次重構我們的convertView 及尋找ImageView和TextView,可以提高app運行速度
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.application_layout, null);
holder = new ViewHolder();
holder.mAppIcon = (ImageView)convertView.findViewById(R.id.app_icon);
holder.mAppTitle = (TextView)convertView.findViewById(R.id.app_title);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
ResolveInfo res = resInfo.get(position);
holder.mAppIcon.setImageDrawable(res.loadIcon(mPackageManager));
holder.mAppTitle.setText(res.loadLabel(mPackageManager).toString());
return convertView;
}
private class ViewHolder {
ImageView mAppIcon;
TextView mAppTitle;
}
}
}
隨著Android設備增多,不少網站都開始設備Android設備,而Android主流設備類型以手機和平板為主。網站在適配時通過User Agent(用戶代理,以下簡稱U
TabLayout的兩種常見設計布局:頂部標簽頁(如今日頭條,知乎) 底部菜單欄(如微信主界面)接著接著舉例使用TabLayout+ViewPager來簡單實現一個類似於
最近公司的產品在陸續做升級,上級領導給的任務是優化代碼結構以及項目架構,力爭把項目寫的精巧簡練,於是我們滿工程找冗余...我們都知道每一個項目基本上都是有登陸頁的,在登陸
搶紅包,先看效果圖~ 實現自動搶紅包,解決問題有兩點: 一:如何實時監聽發紅包的事件 二:如何在紅包到來的時候自動進入頁面並自動點擊紅包一、如何獲取紅包到來的事件