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

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

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

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

<?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

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代码:

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代码:

<?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文件代码:

<?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代码:

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代码:

<?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代码:

<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;

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

(0)

相关推荐

  • Android中AlertDialog各种对话框的用法实例详解

    目标效果: 程序运行,显示图一的几个按钮,点击按钮分别显示图二到图六的对话框,点击对话框的某一项或者按钮,也会显示相应的吐司输出. 1.activity_main.xml页面存放五个按钮. activity_main.xml页面: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools&

  • Android开发实现模仿微信小窗口功能【Dialog对话框风格窗口】

    本文实例讲述了Android开发实现模仿微信小窗口功能.分享给大家供大家参考,具体如下: 运用方法: 将显示窗口的风格 设置为对话框风格即可 具体效果: 具体实现: 首先我们先定义布局文件: <?xml version="1.0" encoding="utf-8" ?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" andro

  • android仿支付宝、微信密码输入框效果

    本文实例为大家分享了android密码输入框效果展示的具体代码,供大家参考,具体内容如下 老规矩,先看效果:这是现在商城类app中很常见的支付密码效果,首先说下这个不是自定义控件,是github上的开源库: https://github.com/Jungerr/GridPasswordView 下面主要说下这个开源库的用法和平时我们常用的几点功能: 想要使用这个开源库首先我们需要将库导入到咱们的项目中: 直接在app的build.gradle下添加如下代码 dependencies { comp

  • Android中自定义对话框(Dialog)的实例代码

    1.修改系统默认的Dialog样式(风格.主题) 2.自定义Dialog布局文件 3.可以自己封装一个类,继承自Dialog或者直接使用Dialog类来实现,为了方便以后重复使用,建议自己封装一个Dialog类 第一步: 我们知道Android定义个控件或View的样式都是通过定义其style来实现的,查看Android框架中的主题文件,在源码中的路径:/frameworks/base/core/res/res/values/themes.xml,我们可以看到,Android为Dialog定义了

  • Android仿微信和QQ多图合并框架(类似群头像)的实现方法

    前言 现在多数app里面加入聊天已经是一个非常普遍的现象了,而微信和qq则是通讯领域的鼻祖了.如果产品经理在考虑做聊天设计的时候,多数会参考. 常常你会听到,你看微信和qq都是这么做的,你就这么来吧,虽然心理有一万个不痛快,但谁叫我们是有一个有追求的程序员呢. 所以产品的要求是实现类似微信的群头像. 类似如下 作为程序员,首先会评估下工作量吧.在产品眼里,就是把图片合成一起嘛,有啥难度吗?所以工作时间决定了你能做成什么样吧 方案分析: 方案1.直接写成布局,然后按照不同的布局加载不同张数的图片.

  • Android 类似微信登录输入框效果

    微信的登录输入框效果如下 进入自动打开自动启动软键盘 点击下一个输入框,下划线颜色改变 怎么实现这样的效果呢,其实非常简单! 简单的布局我就不说了,直接上干货. 1.实现进入自动弹出软键盘,在根文件中的Activity中设置 windowSoftInputMode 属性为 stateVisible|adjustResize 例如 <activity android:name=".SetLoginPasswordActivity" android:windowSoftInputMo

  • Android仿QQ、微信聊天界面长按提示框效果

    先来看看效果图 如何使用 示例代码 PromptViewHelper pvHelper = new PromptViewHelper(mActivity); pvHelper.setPromptViewManager(new ChatPromptViewManager(mActivity)); pvHelper.addPrompt(holder.itemView.findViewById(R.id.textview_content)); 使用起来还是很简单的 首先new一个PromptViewH

  • Android 使用<layer-list>实现微信聊天输入框功能

    LayerDrawable  <layer-list> 标签可是设置LayerDrawable,一种有层次的Drawable叠加效果,<layer-list> 可以包含多个 <item>标签, 每个 <item>代表一个Drawable.<item>可以通过left.right.top.bottom设置左右上下的偏移量,<item>可以应用一个图片,也可以是一个shape 我们来模仿实现微信的聊天输入框: 先设置绿色的背景: <

  • Android 仿微信朋友圈点赞和评论弹出框功能

    贡献/下载源码:https://github.com/mmlovesyy/PopupWindowDemo 本文简单模仿微信朋友圈的点赞和评论弹出框,布局等细节请忽略,着重实现弹出框.发评论,及弹出位置的控制. 1. 微信弹出框 微信朋友圈的点赞和评论功能,有2个组成部分: 点击左下角的"更多"按钮,弹出对话框: 点击评论,弹出输入框,添加评论并在页面中实时显示: 微信朋友圈点赞和评论功能 2. 实际效果 本文将建一个 ListView,在其 Item 中简单模仿微信的布局,然后着重实现

  • Android实现点击AlertDialog上按钮时不关闭对话框的方法

    本文实例讲述了Android实现点击AlertDialog上按钮时不关闭对话框的方法.分享给大家供大家参考.具体如下: 开发过程中,有时候会有这样的需求: 点击某个按钮之后显示一个对话框,对话框上面有一个输入框,并且有"确认"和"取消"两个按钮.当用户点击确认按钮时,需要对输入框的内容进行判断.如果内容为空则不关闭对话框,并toast提示. 使用AlertDialog.Builder创建对话框时,可以使用builder.setNegativeButton和build

随机推荐