<intent-filter>
</intent-filter>
中添加
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
作用
把应用程序作为home
知识点:GridView 显示数据表格(一种控件) 宫格布局
GridView属性说明
android:columnWidth
设置列的宽度
android:horizontalSpacing
两列之间的间距
android:verticalSpacing
两行之间的间距
android:numColumns
设置列数。取值有2种。
(01)可以设置为数字,比如:”2”表示有两列。
(02)可以设置为“auto_fit”,表示根据“屏幕宽
度”和“GridView宽度”来动态匹配。
渲染问题解决办法:在项目项目中找到 app src main res values styles.xml文件,把下面这一栏
<style name =“AppTheme”parent =“Theme.AppCompat.Light.DarkActionBar”>
改为
<style name =“AppTheme”parent =“Base.Theme.AppCompat.Light.DarkActionBar”>
https://blog.csdn.net/dafei93/article/details/80471615
activity_main.xml
加入宫格布局 GridView
<GridView
android:id="@+id/apps_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="180dp"
android:numColumns="4"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
MainActivity.java
package com.example.administrator.cloverlauncher;
import java.util.List;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
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;
public class MainActivity extends Activity {
private List<ResolveInfo> mApps;
private GridView mGrid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loadApps();
setContentView(R.layout.activity_main);
mGrid = (GridView) findViewById(R.id.apps_list);
mGrid.setAdapter(new AppsAdapter());
mGrid.setOnItemClickListener(listener);
}
private OnItemClickListener listener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
ResolveInfo info = mApps.get(position);
//该应用的包名
String pkg = info.activityInfo.packageName;
//应用的主activity类
String cls = info.activityInfo.name;
ComponentName componet = new ComponentName(pkg, cls);
Intent i = new Intent();
i.setComponent(componet);
startActivity(i);
}
};
private void loadApps() {
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mApps = getPackageManager().queryIntentActivities(mainIntent, 0);
}
public class AppsAdapter extends BaseAdapter {
public AppsAdapter() {
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i;
if (convertView == null) {
i = new ImageView(MainActivity.this);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new GridView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
} else {
i = (ImageView) convertView;
}
ResolveInfo info = mApps.get(position);
i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));
return i;
}
public final int getCount() {
return mApps.size();
}
public final Object getItem(int position) {
return mApps.get(position);
}
public final long getItemId(int position) {
return position;
}
}
}