Android知识点六:自定义分享菜单

     首先布局:activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:gravity="center_horizontal">

    <Button
        android:id="@+id/btn_click"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="10dip"
        android:background="@drawable/btn_black_selector"
        android:focusable="false"
        android:text="点击"
        android:textColor="@color/white"
        android:textSize="18sp" />

    <RelativeLayout
        android:id="@+id/myRelativeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/popup_bg_black"
        android:gravity="bottom|center_horizontal"
        android:orientation="vertical"
         >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip"
            android:background="@drawable/popup_bg_page"
            android:orientation="vertical" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:paddingBottom="10dip"
                android:paddingTop="15dip"
                android:text="选择分享方式"
                android:textColor="@color/white"
                android:textSize="16sp" />

            <com.tbkj.common.widget.GridViewInScrowView
                android:id="@+id/sharetype_grid"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:listSelector="#00000000"
                android:numColumns="3" >
            </com.tbkj.common.widget.GridViewInScrowView>

            <Button
                android:id="@+id/cancel_btn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginBottom="10dip"
                android:background="@drawable/btn_black_selector"
                android:focusable="false"
                android:text="取消"
                android:textColor="@color/white"
                android:textSize="18sp" />
        </LinearLayout>
    </RelativeLayout>

</RelativeLayout>

   自定义控件GridViewInScrowView继承GridView

 

public class GridViewInScrowView extends GridView {

	public GridViewInScrowView(Context context) {
		super(context);
	}

	public GridViewInScrowView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public GridViewInScrowView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
				MeasureSpec.AT_MOST);
		super.onMeasure(widthMeasureSpec, expandSpec);
	}

}

MainActivity.java

public class MainActivity extends Activity implements OnClickListener, OnItemClickListener {

	public static final String SinaWeiBo = "SinaWeibo";
	public static final String TencentWeiBo = "TencentWeibo";
	public static final String Wechat = "Wechat";
	public static final String WechatMoments = "WechatMoments";

	private GridViewInScrowView mShareView;

	private List<Map<String, Object>> data;
	private Button mButton, cButton;
	private RelativeLayout myRelativeLayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		ShareSDK.initSDK(this);
		InitView();
		InitListener();
		InitData();
	}

	void InitView() {
		mButton = (Button) findViewById(R.id.btn_click);
		cButton = (Button) findViewById(R.id.cancel_btn);
		myRelativeLayout = (RelativeLayout) findViewById(R.id.myRelativeLayout);

		mShareView = (GridViewInScrowView) findViewById(R.id.sharetype_grid);
	}

	void InitListener() {
		mButton.setOnClickListener(this);
		cButton.setOnClickListener(this);
	}

	void InitData() {

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_click:
			myRelativeLayout.setVisibility(View.VISIBLE);
			initGridView();
			break;
		case R.id.cancel_btn:
			myRelativeLayout.setVisibility(View.GONE);
			break;
		default:
			break;
		}
	}

	@SuppressWarnings("serial")
	private void initGridView() {
		data = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>() {
			{
				put("image", R.drawable.ico_sina);
				put("text", "新浪微博");
				put("plantform", SinaWeiBo);
			}
		};

		data.add(map);
		map = new HashMap<String, Object>() {
			{
				put("image", R.drawable.ico_microletter);
				put("text", "微信好友");
				put("plantform", Wechat);
			}
		};
		data.add(map);
		map = new HashMap<String, Object>() {
			{
				put("image", R.drawable.ico_weibo);
				put("text", "腾讯微博");
				put("plantform", TencentWeiBo);
			}
		};
		data.add(map);
		map = new HashMap<String, Object>() {
			{
				put("image", R.drawable.ico_microletter_friend);
				put("text", "微信朋友圈");
				put("plantform", WechatMoments);
			}
		};
		data.add(map);
		SimpleAdapter mAdapter = new SimpleAdapter(this, data, R.layout.shelf_good_details_share_item, new String[] { "image", "text" }, new int[] { R.id.item_img, R.id.item_text });
		mShareView.setAdapter(mAdapter);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Map<String, Object> map = data.get(position);
		String mPlantform = map.get("plantform").toString();
		if (mPlantform.equals(TencentWeiBo)) {
			Platform platform = ShareSDK.getPlatform(MainActivity.this, TencentWeiBo);
		} else if (mPlantform.equals(MainActivity.SinaWeiBo)) {
			Platform platform = ShareSDK.getPlatform(MainActivity.this, SinaWeiBo);
		} else if (mPlantform.equals(MainActivity.Wechat)) {
			Platform platform = ShareSDK.getPlatform(MainActivity.this, Wechat);
			return;
		} else if (mPlantform.equals(MainActivity.WechatMoments)) {
			Platform platform = ShareSDK.getPlatform(MainActivity.this, WechatMoments);
			return;
		}
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值