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;
}
}
}
<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 中自定义分享样式弹窗
- 自定义Android菜单背景
- Android自定义View分享——仿微信朋友圈图片合并效果
- android 自定义悬浮菜单
- android 自定义ViewGroup和对view进行切图动画实现滑动菜单SlidingMenu
- 【android开发】类抽屉菜单(自定义ViewGroup)的实现
- Android自定义VIew实现卫星菜单效果浅析
- Android实现自定义分享功能
- 自定义Android滑动式菜单SlidingMenu(不依赖任何第三方库)
- 周末闲着无聊分享一个自己写的带呼吸效果的android水波纹自定义view
- android自定义控件之模仿优酷菜单
- Android 自定义分享列表ACTION_SEND
- 来实现android自定义的menu菜单之二:为自定义menu中的线性布局添加事件
- Android开发手机菜单(menu)按键的自定义
- Android自定义左右菜单(侧滑栏)
- Android开发网上的一些重要知识点[经验分享]
- android中用PopUpWindow自定义Menu菜单 带半透明效果
- android 实现自定义卫星菜单
- Android之自定义 ActionBar 上的菜单(Menu)文字颜色
- iOS 微信分享整理 - 自定义分享菜单