您的位置:首页 > 移动开发 > Android开发

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

2014-03-12 17:48 309 查看
     首先布局: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;
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android