分享意图:自定义布局

时间:2014-12-02 09:24:28

标签: android

有没有办法可以自定义Share Intent的布局?默认是ListView,如果客户端想要一切相同,但要显示网格视图该怎么办!我知道如何使用packageManager过滤所需的应用程序。但我想将ListView更改为gridView,其他一切都相同。

2 个答案:

答案 0 :(得分:3)

根据您的要求调用以下对话框:

private void showAlertDialog() {
        PackageManager pm = getPackageManager();
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        GridView gridView = new GridView(this);

        email.putExtra(Intent.EXTRA_EMAIL,
                new String[] { "velmurugan@androidtoppers.com" });
        email.putExtra(Intent.EXTRA_SUBJECT, "Hi");
        email.putExtra(Intent.EXTRA_TEXT, "Hi,This is Test");
        email.setType("text/plain");
        List<ResolveInfo> launchables = pm.queryIntentActivities(email, 0);

        Collections
                .sort(launchables, new ResolveInfo.DisplayNameComparator(pm));

        adapter = new AppAdapter(pm, launchables);
        gridView.setNumColumns(3);
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {
                // TODO Auto-generated method stub
                ResolveInfo launchable = adapter.getItem(position);
                ActivityInfo activity = launchable.activityInfo;
                ComponentName name = new ComponentName(
                        activity.applicationInfo.packageName, activity.name);
                email.addCategory(Intent.CATEGORY_LAUNCHER);
                email.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                email.setComponent(name);
                startActivity(email);
            }
        });


        builder.setView(gridView);
        builder.setTitle("Share");
        builder.show();
    }

row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:paddingBottom="2dp"
        android:paddingLeft="2dp"
        android:paddingRight="5dp"
        android:paddingTop="2dp" />

    <TextView
        android:id="@+id/label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/icon"
        android:gravity="center_vertical"
        android:paddingBottom="2sp"
        android:paddingTop="4sp"
        android:textColor="@android:color/white"
        android:textSize="11pt" />

</LinearLayout>

适配器类:

class AppAdapter extends ArrayAdapter<ResolveInfo> {
        private PackageManager pm = null;

        AppAdapter(Context context, PackageManager pm, List<ResolveInfo> apps) {
            super(context, R.layout.row, apps);
            this.pm = pm;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = newView(parent);
            }

            bindView(position, convertView);

            return (convertView);
        }

        private View newView(ViewGroup parent) {
            return (getLayoutInflater().inflate(R.layout.row, parent, false));
        }

        private void bindView(int position, View row) {
            TextView label = (TextView) row.findViewById(R.id.label);

            label.setText(getItem(position).loadLabel(pm));

            ImageView icon = (ImageView) row.findViewById(R.id.icon);

            icon.setImageDrawable(getItem(position).loadIcon(pm));
        }
    }

答案 1 :(得分:1)

我不认为你可以开箱即用。因此,您需要使用网格创建自己的共享对话框。 这里有一些示例https://github.com/xinanadu/CustomShare/blob/master/app/src/main/java/info/zhegui/share/MainActivity.java