Android底部缓慢弹出菜单实例代码介绍

来源:爱站网时间:2023-01-14编辑:网友分享
这篇文章主要介绍了Android底部缓慢弹出菜单实例代码内容,不知道朋友们有没有兴趣了解下,如果有,一定要看看下列的文章,希望技术小编提供的知识点对你有帮助。

项目要求要做一个从底部缓慢弹出一个Button Menu

开始编码:
MyselfFragment

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206      

207

208

209

210

211

212

213

package io.dcloud.H5B79C397.fragment;

 

import android.content.Context;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.Button;

import android.widget.RelativeLayout;

import android.widget.Toast;

 

import com.android.volley.VolleyError;

import com.vanda.vandalibnetwork.fragment.BaseFragment;

 

import java.util.Map;

 

import cn.sharesdk.framework.ShareSDK;

import cn.sharesdk.onekeyshare.OnekeyShare;

import cn.sharesdk.sina.weibo.SinaWeibo;

import cn.sharesdk.tencent.qq.QQ;

import cn.sharesdk.wechat.friends.Wechat;

import cn.sharesdk.wechat.moments.WechatMoments;

import io.dcloud.H5B79C397.R;

import io.dcloud.H5B79C397.Urls;

import io.dcloud.H5B79C397.activity.BottomMenu;

import io.dcloud.H5B79C397.activity.LoginActivity;

import io.dcloud.H5B79C397.activity.Myself_About_fae;

import io.dcloud.H5B79C397.activity.Myself_Invitation;

import io.dcloud.H5B79C397.activity.Myself_help;

import io.dcloud.H5B79C397.activity.Myself_setting;

import io.dcloud.H5B79C397.activity.RegisterActivity;

import io.dcloud.H5B79C397.activity.user_management;

import io.dcloud.H5B79C397.pojo.InformmationData;

import io.dcloud.H5B79C397.util.ActionBarUtil;

import io.dcloud.H5B79C397.util.ExtUtils;

 

/**

 * 用户个人信息界面

 */

public class MyselfFragment extends BaseFragment<InformmationData> implements View.OnClickListener {

  private Context mContext;

  private View view;

  private BottomMenu menuWindow;

  private RelativeLayout layout_advise, layout_collect, layout_logo, layout_share, layout_qr_code, layout_help, layout_set, layout_url;

  private Button btnLogin, btnRegister;

 

  @Override

  public View onCreateView(LayoutInflater inflater, ViewGroup container,

               Bundle savedInstanceState) {

    view = inflater.inflate(R.layout.fragment_information, container, false);

 

    initView();

    return view;

 

  }

 

  @Override

  public void onStart() {

    super.onStart();

  }

 

  private void initView() {

    new ActionBarUtil(view, R.id.actionBar, R.id.Back, R.id.Title, R.id.Activity, "我的管理中心", getActivity(), 2);

 

    layout_advise = (RelativeLayout) view.findViewById(R.id.myself_advise);

    layout_collect = (RelativeLayout) view.findViewById(R.id.myself_collect);

    layout_logo = (RelativeLayout) view.findViewById(R.id.myself_logo);

    layout_share = (RelativeLayout) view.findViewById(R.id.myself_share);

    layout_qr_code = (RelativeLayout) view.findViewById(R.id.myself_qr_code);

    layout_help = (RelativeLayout) view.findViewById(R.id.myself_help);

    layout_set = (RelativeLayout) view.findViewById(R.id.myself_set);

    layout_url = (RelativeLayout) view.findViewById(R.id.myself_url);

 

    btnLogin = (Button) view.findViewById(R.id.myself_login);

    btnRegister = (Button) view.findViewById(R.id.myself_register);

 

    layout_advise.setOnClickListener(this);

    layout_collect.setOnClickListener(this);

    layout_logo.setOnClickListener(this);

    layout_share.setOnClickListener(this);

    layout_qr_code.setOnClickListener(this);

    layout_help.setOnClickListener(this);

    layout_set.setOnClickListener(this);

    layout_url.setOnClickListener(this);

    btnLogin.setOnClickListener(this);

    btnRegister.setOnClickListener(this);

  }

 

 

  @Override

  public void onClick(View v) {

    switch (v.getId()) {

      case R.id.myself_advise:

        startActivity(new Intent(getActivity(), user_management.class));

        break;

      case R.id.myself_collect:

        break;

      case R.id.myself_logo:

        Intent intent=new Intent(getActivity(),Myself_About_fae.class);

        startActivity(intent);

        break;

      case R.id.myself_share:

        menuWindow = new BottomMenu(mContext, clickListener);

        menuWindow.show();

 

        break;

      case R.id.myself_qr_code:

        startActivity(new Intent(getActivity(), Myself_Invitation.class));

        break;

      case R.id.myself_help:

        startActivity(new Intent(getActivity(), Myself_help.class));

        break;

      case R.id.myself_set:

          startActivity(new Intent(getActivity(), Myself_setting.class));

        break;

      case R.id.myself_url:

        Uri uri = Uri.parse("http://www.fae.cn/");

        Intent intent1 = new Intent(Intent.ACTION_VIEW, uri);

        startActivity(intent1);

        break;

      case R.id.myself_login:

        startActivity(new Intent(getActivity(), LoginActivity.class));

        break;

      case R.id.myself_register:

        startActivity(new Intent(getActivity(), RegisterActivity.class));

        break;

      default:

        break;

    }

  }

  private View.OnClickListener clickListener = new View.OnClickListener(){

    public void onClick(View v) {

      switch (v.getId()) {

        case R.id.btn1:

          showZiDingYi(Wechat.NAME);

          break;

        case R.id.btn2:

          showZiDingYi(WechatMoments.NAME);

          Toast.makeText(mContext,"这是微信朋友圈",Toast.LENGTH_SHORT).show();

          break;

        case R.id.sina:

          showZiDingYi(SinaWeibo.NAME);

          break;

        case R.id.QQ:

          showZiDingYi(QQ.NAME);

          break;

        case R.id.more:

          Toast.makeText(mContext,"这是更多",Toast.LENGTH_SHORT).show();

 

          break;

        default:

          break;

      }

    }

  };

  private void showZiDingYi(String ss){

    ShareSDK.initSDK(getActivity());

    OnekeyShare oks = new OnekeyShare();

    oks.setSilent(false);

    oks.setDialogMode();

    oks.setText("xxxxxxxxx");

    //oks.setImageUrl("http://sharesdk.cn");

    oks.setPlatform(ss);

    oks.show(getActivity());

  }

 

  public static MyselfFragment newInstance(Context context) {

    MyselfFragment fragment = new MyselfFragment();

    fragment.mContext = context;

    return fragment;

  }

 

  public MyselfFragment() {

 

    // Required empty public constructor

 

  }

 

 

  @Override

  protected void processData(InformmationData response) {

    super.processData(response);

    if (response != null) {

 

 

    }

 

  }

 

  @Override

  protected void errorData(VolleyError volleyError) {

    super.errorData(volleyError);

    ExtUtils.errorLog("-----MyselfFragment----volleyError--------->", volleyError + "");

  }

 

  @Override

  protected String getRequestUrl() {

    return Urls.URL_PREFIX + "";

  }

 

  @Override

  protected Class<InformmationData> getResponseDataClass() {

    return InformmationData.class;

  }

 

  @Override

  protected Map<String, String> getParamMap() {

    return null;

  }

 

}

BottomMenu

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85     

package io.dcloud.H5B79C397.activity;

 

import android.app.ActionBar;

import android.app.Activity;

import android.content.Context;

import android.graphics.drawable.ColorDrawable;

import android.view.Gravity;

import android.view.LayoutInflater;

import android.view.MotionEvent;

import android.view.View;

import android.view.ViewGroup;

import android.widget.Button;

import android.widget.PopupWindow;

 

import io.dcloud.H5B79C397.R;

 

import static io.dcloud.H5B79C397.R.id.btn1;

import static io.dcloud.H5B79C397.R.id.btn2;

 

/**

 * Created by Administrator on 2017/7/21 0021.

 */

 

public class BottomMenu implements View.OnClickListener,View.OnTouchListener {

  private PopupWindow popupWindow;

  private Button wechat, friend,qq,sina,more ,btnCancel;

  private View mMenuView;

  private Activity mContext;

  private View.OnClickListener clickListener;

 

  public BottomMenu(Context context, View.OnClickListener clickListener) {

    LayoutInflater inflater = LayoutInflater.from(context);

    this.clickListener=clickListener;

    mContext= (Activity) context;

    mMenuView = inflater.inflate(R.layout.layout_popwindow, null);

    wechat= (Button) mMenuView.findViewById(btn1);

    friend= (Button) mMenuView.findViewById(btn2);

    qq=(Button)mMenuView.findViewById(R.id.QQ) ;

    sina=(Button)mMenuView.findViewById(R.id.sina);

    more=(Button)mMenuView.findViewById(R.id.more);

    btnCancel = (Button) mMenuView.findViewById(R.id.btn_cancel);

    btnCancel.setOnClickListener(this);

    wechat.setOnClickListener(this);

    friend.setOnClickListener(this);

    qq.setOnClickListener(this);

    sina.setOnClickListener(this);

    more.setOnClickListener(this);

    popupWindow=new PopupWindow(mMenuView, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT,true);

    popupWindow.setAnimationStyle(R.style.popwin_anim_style);

    ColorDrawable dw = new ColorDrawable(context.getResources().getColor(R.color.apha));

    popupWindow.setBackgroundDrawable(dw);

    mMenuView.setOnTouchListener(this);

  }

  /**

   * 显示菜单

   */

  public void show(){

    //得到当前activity的rootView

    View rootView=((ViewGroup)mContext.findViewById(android.R.id.content)).getChildAt(0);

    popupWindow.showAtLocation(rootView, Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);

  }

  @Override

  public void onClick(View view) {

    popupWindow.dismiss();

    switch (view.getId()) {

      case R.id.btn_cancel:

        break;

      default:

        clickListener.onClick(view);

        break;

    }

  }

  @Override

  public boolean onTouch(View arg0, MotionEvent event) {

    int height = mMenuView.findViewById(R.id.pop_layout).getTop();

    int y=(int) event.getY();

    if(event.getAction()==MotionEvent.ACTION_UP){

      if(y<height){

        popupWindow. dismiss();

      }

    }

    return true;

  }

 

}

layout_popwindow.xml

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

  android:layout_width="fill_parent"

  android:layout_height="wrap_content"

  android:gravity="center_horizontal"

  android:orientation="vertical" >

 

  <LinearLayout

    android:id="@+id/pop_layout"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:layout_alignParentBottom="true"

    android:gravity="center_horizontal"

    android:layout_marginLeft="20dp"

    android:layout_marginRight="20dp"

    android:orientation="vertical" >

 

    <Button

      android:background="#F2F2F2"

      android:id="@+id/btn1"

      android:layout_width="match_parent"

      android:layout_height="40dp"

      android:text="分享到微信好友"

      android:textColor="@color/black"

       />

 

    <Button

      android:id="@+id/btn2"

      android:layout_width="match_parent"

      android:layout_height="40dp"

      android:text="分享到微信朋友圈"

      android:layout_marginTop="5dp"

      android:textColor="@color/black"

      android:background="#F2F2F2"

      />

    <Button

      android:id="@+id/QQ"

      android:layout_width="match_parent"

      android:layout_height="40dp"

      android:text="分享到QQ"

      android:layout_marginTop="5dp"

      android:textColor="@color/black"

      android:background="#F2F2F2"

      />

    <Button

      android:id="@+id/sina"

      android:layout_width="match_parent"

      android:layout_height="40dp"

      android:text="分享到新浪微博"

      android:layout_marginTop="5dp"

      android:textColor="@color/black"

      android:background="#F2F2F2"

      />

    <Button

      android:id="@+id/more"

      android:layout_width="match_parent"

      android:layout_height="40dp"

      android:text="分享到更多..."

      android:layout_marginTop="5dp"

      android:textColor="@color/black"

      android:background="#F2F2F2"

      />

 

 

    <Button

      android:id="@+id/btn_cancel"

      android:layout_width="match_parent"

      android:layout_height="50dp"

      android:text="取消"

      android:layout_marginBottom="10dp"

      android:layout_marginTop="15dp"

      android:background="#F2F2F2"

      android:alpha="100"

      android:textStyle="bold" />

  </LinearLayout>

</RelativeLayout>

包anim//放在res目录下其中包括ppwindow_hide_anim.xml和ppwindow_show_anim.xml
其中ppwindow_hide_anim.xml

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">

  <translate

    android:duration="300"

    android:fromXDelta="0"

    android:toXDelta="0"

    android:fromYDelta="0"

    android:toYDelta="1920"

    />

  <alpha

    android:duration="300"

    android:fromAlpha="1"

    android:toAlpha="0"

    />

 

</set>

其中ppwindow_show_anim.xml

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">

  <translate

    android:duration="300"

    android:fromXDelta="0"

    android:toXDelta="0"

    android:fromYDelta="1920"

    android:toYDelta="0"

    />

  <alpha

    android:duration="300"

    android:fromAlpha="0"

    android:toAlpha="1"

    />

 

</set>

最后上图:

以上就是Android底部缓慢弹出菜单实例代码全部内容了,希望你们希望小编所整理的问题,如果有疑问,欢迎来网站平台咨询小编,更多精彩内容尽在js.aizhan.com。

上一篇:android环形进度条实例代码

下一篇:Android底部图片选择应该怎么布局

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载