服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android实现微信右侧顶部下拉对话框

Android实现微信右侧顶部下拉对话框

2022-09-05 15:40hfut_why Android

这篇文章主要为大家详细介绍了Android实现微信右侧顶部下拉对话框,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

我们使用微信都知道,其右侧顶部有一个下拉对话框,我们可以执行添加好友,扫一扫等功能,今天我们就来模仿实现一下这个功能(实现的方式有很多种,我今天只说一种借助透明主题Activity的方式实现;如果有兴趣还可以移步至仿淘宝底部导航栏);本篇的实现的效果如下:

Android实现微信右侧顶部下拉对话框

下面就来说一说实现的思路重要

第一步:创建弹出对话框布局

?
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
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" >
 
 <RelativeLayout
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_marginTop="45dp"
  android:layout_marginRight="20dp">
 
  <LinearLayout
   android:id="@+id/id_pop_dialog_layout"
   android:layout_width="@dimen/pop_list_width"
   android:layout_height="wrap_content"
   android:layout_alignParentRight="true"
   android:layout_alignParentTop="true"
   android:background="@drawable/pop_item_normal"
   android:orientation="vertical" >
 
   <LinearLayout
    android:id="@+id/upload_record_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="5dp"
    android:background="@drawable/pop_list_selector" >
 
    <ImageView
     android:id="@+id/id_imageView1"
     android:layout_width="@dimen/pop_dialog_icon_size"
     android:layout_height="@dimen/pop_dialog_icon_size"
     android:layout_gravity="center_vertical"
     android:layout_marginLeft="8dp"
     android:src="@drawable/upload_icon_record" />
 
    <TextView
     android:layout_width="@dimen/pop_list_width"
     android:layout_height="wrap_content"
     android:padding="8dp"
     android:text="@string/uploadRecord"
      android:layout_gravity="center_vertical"
     android:textColor="#fff"
     android:textSize="16sp" />
   </LinearLayout>
 
   <ImageView
    android:id="@+id/id_imageView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/pop_line" />
 
   <LinearLayout
    android:id="@+id/register_record_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:background="@drawable/pop_list_selector" >
 
    <ImageView
     android:id="@+id/id_imageView2"
     android:layout_width="@dimen/pop_dialog_icon_size"
     android:layout_height="@dimen/pop_dialog_icon_size"
     android:layout_gravity="center_vertical"
     android:layout_marginLeft="8dp"
     android:src="@drawable/register_icon_record" />
 
    <TextView
     android:layout_width="@dimen/pop_list_width"
     android:layout_height="wrap_content"
     android:padding="8dp"
     android:text="@string/registerRecord"
      android:layout_gravity="center_vertical"
     android:textColor="#fff"
     android:textSize="16sp" />
   </LinearLayout>
 
   <ImageView
    android:id="@+id/id_imageView7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/pop_line" />
 
   <LinearLayout
    android:id="@+id/new_massage_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:background="@drawable/pop_list_selector" >
 
    <ImageView
     android:id="@+id/id_imageView3"
     android:layout_width="@dimen/pop_dialog_icon_size"
     android:layout_height="@dimen/pop_dialog_icon_size"
     android:layout_gravity="center_vertical"
     android:layout_marginLeft="8dp"
     android:src="@drawable/message_icon_tip" />
 
    <TextView
     android:id="@+id/new_message"
     android:layout_width="@dimen/pop_list_width"
     android:layout_height="wrap_content"
     android:padding="8dp"
     android:text="@string/defaultMessage"
     android:layout_gravity="center_vertical"
     android:textColor="#fff"
     android:textSize="16sp" />
   </LinearLayout>
 
   <ImageView
    android:id="@+id/id_imageView6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/pop_line" />
 
  </LinearLayout>
 </RelativeLayout>
 
</RelativeLayout>

第二步:创建一个用于显示该对话框布局Activity

?
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
package com.hfut.popdialogtest;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.LinearLayout;
 
/**
 * @author why
 * @date 2018-10-3
 */
public class MyDialogActivity extends Activity implements OnClickListener{
 
 private LinearLayout uploadRecord;
 private LinearLayout registerRecord;
 private LinearLayout newMessage;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  setContentView(R.layout.pop_dialog);
 
  if(getActionBar()!=null){
   getActionBar().hide();
  }
  CommonTools.setNavbarVisibility(this);
  initView();
 }
 
 
 private void initView(){
  uploadRecord = findViewById(R.id.upload_record_layout);
  registerRecord = findViewById(R.id.register_record_layout);
  newMessage = findViewById(R.id.new_massage_layout);
 
  uploadRecord.setOnClickListener(this);
  registerRecord.setOnClickListener(this);
  newMessage.setOnClickListener(this);
 }
 
 @Override
 public boolean onTouchEvent(MotionEvent event){
  finish();
  return true;
 }
 
 @Override
 public void onClick(View v) {
  switch (v.getId()){
   case R.id.upload_record_layout:
   SharedData.resultID=1;
   break;
   case R.id.register_record_layout:
   SharedData.resultID=2;
   break;
   case R.id.new_massage_layout:
   SharedData.resultID=3;
   break;
   default:
   SharedData.resultID=0;
   break;
  }
  this.finish();
 }
}

第三步:创建一个主界面
MainActivity.java代码:

?
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
package com.hfut.popdialogtest;
 
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
 
/**
 * @author why
 * @date 2018-10-3 9:35:35
 */
public class MainActivity extends AppCompatActivity {
 
 TextView resultShow;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  resultShow = findViewById(R.id.show_choosen_result);
 
  if(getActionBar()!=null){
   getActionBar().hide();
  }
  CommonTools.setNavbarVisibility(this);
 }
 
 
 @Override
 protected void onResume() {
  switch (SharedData.resultID) {
   case 0:
    resultShow.setText("默认显示");
    break;
   case 1:
    resultShow.setText(getResources().getString(R.string.uploadRecord));
    break;
   case 2:
    resultShow.setText(getResources().getString(R.string.registerRecord));
    break;
   case 3:
    resultShow.setText(getResources().getString(R.string.defaultMessage));
    break;
   default:
    resultShow.setText("默认显示");
    break;
 
  }
  super.onResume();
 }
 
 public void openPopDialog(View view) {
  Intent intent = new Intent(this, PopDialogActivity.class);
  startActivity(intent);
 }
}

activity_main.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
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.hfut.popdialogtest.MainActivity">
 
 <ImageView
  android:onClick="openPopDialog"
  android:id="@+id/pop_dialog_icon"
  app:layout_constraintRight_toRightOf="parent"
  android:layout_marginRight="10dp"
  app:layout_constraintTop_toTopOf="parent"
  android:layout_marginTop="5dp"
  android:background="@drawable/message_tip"
  android:layout_width="50dp"
  android:layout_height="50dp" />
 
 <TextView
  android:gravity="center"
  android:textColor="@color/colorAccent"
  android:textSize="30sp"
  android:id="@+id/show_choosen_result"
  app:layout_constraintTop_toBottomOf="@id/pop_dialog_icon"
  android:layout_marginTop="50dp"
  android:layout_width="match_parent"
  android:layout_height="match_parent" />
 
</android.support.constraint.ConstraintLayout>

第四步:设置对话框Activity主题为透明主题
AndroidManifest.xml文件代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.hfut.popdialogtest">
 
 <application
  android:allowBackup="true"
  android:icon="@mipmap/ic_launcher"
  android:label="@string/app_name"
  android:roundIcon="@mipmap/ic_launcher_round"
  android:supportsRtl="true"
  android:theme="@style/AppTheme">
  <activity android:name=".MainActivity">
   <intent-filter>
    <action android:name="android.intent.action.MAIN" />
 
    <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
  </activity>
  <activity android:name=".MyDialogActivity" android:theme="@android:style/Theme.Translucent" />
 </application>
 
</manifest>

第五步:其他辅助代码
CommonTools.java代码:

?
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
package com.hfut.popdialogtest;
 
import android.app.Activity;
import android.view.View;
 
/**
 * author:why
 * created on: 2018/9/11 13:34
 * description:
 */
public class CommonTools {
 
 /**
  * to controll the visibility of the Activity's navigator bar
  * @param activity
  */
 public static void setNavbarVisibility(Activity activity) {
  View decorView = activity.getWindow().getDecorView();
  decorView.setSystemUiVisibility(
    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
      | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
      | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
      | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
      | View.SYSTEM_UI_FLAG_FULLSCREEN
      | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
 }
 
}

Values目录下的dimens.xml代码:

?
1
2
3
4
5
6
<?xml version="1.0" encoding="utf-8"?>
<resources>
 <dimen name="pop_list_width">160dp</dimen>
 <dimen name="pop_dialog_icon_size">60dp</dimen>
 <dimen name="pop_dialog_icon_tip_size">40dp</dimen>
</resources>

Values目录下的strings.xml代码:

?
1
2
3
4
5
6
7
8
<resources>
 <string name="app_name">仿微信右侧顶部下拉弹出测试</string>
 
 <string name="uploadRecord">上传记录</string>
 <string name="registerRecord">注册记录</string>
 <string name="defaultMessage">消息提示</string>
 
</resources>

其他资源文件就不添加了。我们总结一下其实就是这样的步骤:

  • 点击主Activity的弹窗对话框图标,打开一个新的透明的Acitivity
  • 在新的Activity中做完逻辑处理把结果存放在主Activity可访问的数据域,然后finish自己
  • 主Activity再次可交互,并在onResume中实现对处理结果分析和处理,比如修改主Activity UI; 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/hfut_why/article/details/82930269

延伸 · 阅读

精彩推荐