回答here:
Intent i=new Intent(android.content.Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(android.content.Intent.EXTRA_SUBJECT,"Subject test");
i.putExtra(android.content.Intent.EXTRA_TEXT, "extra text that you want to put");
startActivity(Intent.createChooser(i,"Share via"));
在Lollipop上它给了我类似this(一个垂直列表)的东西。
但有时我会看到像this这样的网格布局。例如:Youtube App的“分享视频”。
为什么布局不同?如何获取共享对话框的网格布局?
答案 0 :(得分:2)
您必须像这样创建自定义选择器。
public class ShareAdapter extends BaseAdapter {
protected static final String TAG = "ShareAdapter";
Intent sendIntent = new Intent(Intent.ACTION_SEND);
protected Context context;
protected List<ResolveInfo> list = new ArrayList<>();
protected PackageManager pm;
public ShareAdapter(Context context) {
Log.d(TAG, "ShareAdapter");
this.context = context;
pm = context.getPackageManager();
sendIntent.setType("image/*");
list = pm.queryIntentActivities(sendIntent, 0);
}
public void updateList(@NonNull List<ResolveInfo> list) {
this.list = list;
notifyDataSetChanged();
}
@Override
public int getCount() {
return list.size();
}
@Override
public ResolveInfo getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_chooser, parent, false);
holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.name = (TextView) convertView.findViewById(R.id.name);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.icon.setImageDrawable(getItem(position).loadIcon(pm));
holder.name.setText(getItem(position).loadLabel(pm));
return convertView;
}
static class ViewHolder {
ImageView icon;
TextView name;
}
}
public void showCustomChooser(final Uri uri) {
ShareAdapter shareAdapter = new ShareAdapter(context);
dialog = new Dialog(ShareActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
WindowManager.LayoutParams WMLP = dialog.getWindow().getAttributes();
WMLP.gravity = Gravity.CENTER;
dialog.getWindow().setAttributes(WMLP);
dialog.getWindow().setGravity(Gravity.BOTTOM);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimationBottom;
dialog.setCanceledOnTouchOutside(true);
dialog.setContentView(R.layout.popup_chooser);
dialog.setCancelable(true);
ListView lv = (ListView) dialog.findViewById(R.id.listView);
ImageButton cancel = (ImageButton) dialog.findViewById(R.id.cancel);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
lv.setAdapter(shareAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
ResolveInfo launchable = shareAdapter.getItem(position);
ActivityInfo activity = launchable.activityInfo;
ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.addCategory(Intent.CATEGORY_LAUNCHER);
sendIntent.setType("image/*");
sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
sendIntent.putExtra(Intent.EXTRA_TEXT, "some text");
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sendIntent.setComponent(name);
context.startActivity(sendIntent);
dialog.dismiss();
}
});
dialog.show();
}
popup_chooser
<?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="350dp"
android:layout_margin="0dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:gravity="center_horizontal"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_gravity="center"
android:background="@color/white"
android:orientation="horizontal">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="12dp"
android:gravity="center"
android:text="@string/share_via"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/black_transparent_50" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<ImageButton
android:id="@+id/cancel"
android:layout_width="48dp"
android:layout_height="48dp"
android:background="@drawable/transparent_button_selector"
android:src="@android:drawable/btn_dialog"
android:visibility="gone" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/black_transparent_12" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dividerHeight="1dp"
android:fadeScrollbars="false"></ListView>
</LinearLayout>
</LinearLayout>
Item_chooser
<?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="wrap_content"
android:orientation="horizontal"
android:padding="4dp">
<ImageView
android:id="@+id/icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginLeft="8dp"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="12dp"
android:textColor="@color/black"
android:textSize="20dp" />
</LinearLayout>
答案 1 :(得分:1)
无论该特定手机的用户界面安装了什么。这基于Android版本,OEM,甚至可能是启动器应用程序。除非您想编写自己的选择器活动,否则无法控制它。