android控件大全及用法_学双人舞一步一步教学

2022-11-15 11:18:10 浏览数 (1)

GridView 用于显示二维滚动网格的View。同ListView一样,他的数据项也是从ListAdapter中产生。关于他的使用方法可参见官网的示例GridView 。

下面先了解下GridView中常用到的属性:

1、android:numColumn : 指定GridView的列数,如果指定为auto_fit , 则可能根据控件的大小自动调整每行显示的列数。

2、android:strechMode : 伸缩模式 , 值为none (不可伸缩)、 spacingWidth(伸缩列之间的间隔空隙) 、 columnWidth(每列伸缩大小一样) 、 spacingWidthUniform(均匀拉伸各列之间的距离) 。

3、 android:columnWidth : 指定列宽度。

关于GridView属性的介绍就到这里。今天通过做一个类似于launcher功能的小程序来学习GridView。先看看程序效果:

下面一步一步实现该功能:

1、activity使用的布局文件,文中指定显示3列 。widget_gridview_layout.xml

代码语言:javascript复制
<?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" >

    <GridView
        android:id="@ id/show_app_grid_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="3"
        android:smoothScrollbar="true"
        android:verticalSpacing="10dp" >
    </GridView>

</LinearLayout>

2、GridView中每个Item的布局文件grid_item_view.xml

代码语言:javascript复制
<?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="100dp"
    android:orientation="vertical" >

    <ImageView
        android:id="@ id/application_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/app_name"
        android:src="@drawable/toggle" />

    <TextView
        android:id="@ id/application_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceSmall" />
    <!--
        android:focusable="true"
        android:focusableInTouchMode="true"


    -->

</LinearLayout>

注意:在代码中如果添加了上面注释的部分,则不会响应GridView 的onItemClick 方法。 3、WidgetGridViewActivity.java

代码语言:javascript复制
package com.xy.zt.selfdefinewieget;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
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.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class WidgetGridViewActivity extends Activity {
private GridView mAppGrid;
private BaseAdapter mAdapter;
PackageManager mPackManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.widget_gridview_layout);
init();
}
private void init() {
mAppGrid = (GridView) findViewById(R.id.show_app_grid_view);
mPackManager = getPackageManager();
List<ApplicationInfo> packInfo = mPackManager
.getInstalledApplications(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
mAdapter = new ApplicationAdapter(this, packInfo);
mAppGrid.setAdapter(mAdapter);
mAppGrid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adaper, View v, int position, long id) {
ApplicationInfo pInfo = (ApplicationInfo) mAdapter.getItem(position);
Intent appIntent = mPackManager.getLaunchIntentForPackage(pInfo.packageName);
if (appIntent == null) {
Toast.makeText(v.getContext(), "this is not a launcher activity !...",
Toast.LENGTH_SHORT).show();
return;
}
appIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(appIntent);
}
});
}
private static class ApplicationAdapter extends BaseAdapter {
private List<ApplicationInfo> mAppList;
private LayoutInflater mInflater;
private PackageManager mPackageManager;
private Context mContext;
public ApplicationAdapter(Context context, List<ApplicationInfo> appList) {
if (context == null || appList == null)
throw new IllegalArgumentException(
"exits null arguments  in constructor method of ApplicationAdapter ! ");
mInflater = LayoutInflater.from(context);
mAppList = new ArrayList<ApplicationInfo>();
int len = appList.size();
ApplicationInfo app;
mPackageManager = context.getPackageManager();
for (int i = 0; i < len; i  ) {
app = appList.get(i);
if (mPackageManager.getLaunchIntentForPackage(app.packageName) == null) {
continue;
}
mAppList.add(app);
}
mContext = context;
}
public int getCount() {
return mAppList.size();
}
public Object getItem(int position) {
return mAppList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
AppHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.grid_item_view, null);
holder = new AppHolder();
convertView.setTag(holder);
} else {
holder = (AppHolder) convertView.getTag();
}
holder.mAppIcon = (ImageView) convertView.findViewById(R.id.application_icon);
holder.mAppName = (TextView) convertView.findViewById(R.id.application_name);
initGridViewItem(mAppList.get(position), holder.mAppIcon, holder.mAppName);
return convertView;
}
private void initGridViewItem(ApplicationInfo pInfo, ImageView img, TextView name) {
String appName =
mPackageManager.getApplicationLabel(pInfo).toString();
Drawable appIcon =
mPackageManager.getApplicationIcon(pInfo);
img.setImageDrawable(appIcon);
name.setText(appName);
//            final String packageName = pInfo.packageName;
// name.setOnClickListener(new OnClickListener() {
// public void onClick(View arg0) {
// doLaunchApp(packageName);
// }
// });
// img.setOnClickListener(new OnClickListener() {
// public void onClick(View arg0) {
// doLaunchApp(packageName);
// }
// });
}
// private void doLaunchApp(String packageName) {
// Intent appIntent =
// mPackageManager.getLaunchIntentForPackage(packageName);
// Log.d("app_info", "start app from packageName : "   packageName);
// mContext.startActivity(appIntent);
// }
private static class AppHolder {
public ImageView mAppIcon;
public TextView mAppName;
}
}
}

注意:上述代码中的注释部分的代码,添加后效果类似于XML文件中的android:focusable=”true” .

该部分代码主要使用到了PackageManager。PackManager.getInstalledApplications(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);获取系统安装的应用程序信息。

在ApplicationAdapter的构造函数中有这么一段代码:

代码语言:javascript复制
ApplicationInfo app;
mPackageManager = context.getPackageManager();
for (int i = 0; i < len; i  ) {
app = appList.get(i);
if (mPackageManager.getLaunchIntentForPackage(app.packageName) == null) {
continue;
}
mAppList.add(app);
}

它对初始的List做了一个简单的过滤,使得所有的图标点击后都能够进入响应的app。

下面代码加载应用的名称和图标信息:

代码语言:javascript复制
String appName =
mPackageManager.getApplicationLabel(pInfo).toString();
Drawable appIcon =
mPackageManager.getApplicationIcon(pInfo);

在代码

代码语言:javascript复制
ApplicationInfo pInfo = (ApplicationInfo) mAdapter.getItem(position);
Intent appIntent = mPackManager.getLaunchIntentForPackage(pInfo.packageName);
if (appIntent == null) {
Toast.makeText(v.getContext(), "this is not a launcher activity !...",
Toast.LENGTH_SHORT).show();
return;
}
appIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(appIntent);

中通过packageManager获取category为Launcher 的Activity的intent。 如果intent == null , 给出一个提示(系统中有些应用是没有入口activity的)。

4、在ViewData.java中添加如下内容(此部分内容可选,如有不清楚的地方参见一步一步学android控件(之一) —— 开始篇)

代码语言:javascript复制
public static final int GRID_ID = EXPANDABLE_LIST_VIEW_ID   1;
public static final String GRID_NAME = "GridView";
private static final ViewData mGridView = new ViewData(GRID_NAME,
GRID_ID);
View_Datas.add(mGridView);

WidgetsAdapter的handleItemClicked中添加如下内容:

代码语言:javascript复制
case ViewData.GRID_ID:
intent.setClass(mContext, WidgetGridViewActivity.class);
mContext.startActivity(intent);
break;

以上就是GridView的全部内容,下一个控件progressBar。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/234973.html原文链接:https://javaforall.cn

0 人点赞