android popwindow仿微信右上角弹出框,dialog底部显示

仿微信右上角弹出框

1、利用popwindow实现

2、popwindow的位置居于右上角

新建,弹出popwindow:

/** 弹popwindow **/
<span style="white-space:pre">		</span>tv = (TextView) findViewById(R.id.textView1);
<span style="white-space:pre">		</span>view_pop = LayoutInflater.from(MainActivity.this).inflate(
<span style="white-space:pre">				</span>R.layout.pop_menu, null);
<span style="white-space:pre">		</span>tv.setOnClickListener(new OnClickListener() {


<span style="white-space:pre">			</span>@Override
<span style="white-space:pre">			</span>public void onClick(View arg0) {
<span style="white-space:pre">				</span>if (mPopupwinow == null) {
<span style="white-space:pre">					</span>//新建一个popwindow
<span style="white-space:pre">					</span>mPopupwinow = new PopupWindow(view_pop,
<span style="white-space:pre">							</span>LayoutParams.WRAP_CONTENT,
<span style="white-space:pre">							</span>LayoutParams.WRAP_CONTENT, true);
<span style="white-space:pre">					</span>//设置popwindow的背景颜色
<span style="white-space:pre">					</span>mPopupwinow.setBackgroundDrawable(new ColorDrawable(
<span style="white-space:pre">							</span>0x00000000));
<span style="white-space:pre">				</span>}
<span style="white-space:pre">				</span>//设置popwindow的位置  tv:为微信右上角+号view,居于+号下方
<span style="white-space:pre">				</span>mPopupwinow.showAsDropDown(tv, 0, 0);
<span style="white-space:pre">			</span>}
<span style="white-space:pre">		</span>});
布局pop_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#00000000"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#fff"
        android:gravity="center_vertical"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_marginRight="50dp"
            android:gravity="center_vertical"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="20dp"
                android:src="@drawable/ic_launcher" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:text="发起聊天"
                android:textSize="18sp" />
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#efefef" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center_vertical"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="20dp"
                android:src="@drawable/ic_launcher" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:text="发起聊天"
                android:textSize="18sp" />
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#efefef" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center_vertical"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="20dp"
                android:src="@drawable/ic_launcher" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:text="发起聊天"
                android:textSize="18sp" />
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#efefef" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center_vertical"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="20dp"
                android:src="@drawable/ic_launcher" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:text="发起聊天"
                android:textSize="18sp" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>



效果:

android popwindow仿微信右上角弹出框,dialog底部显示

1、屏幕下方dialog

创建dialog,设置dialog的位置

/** 弹dialog **/
	private void showDialog() {
		dialog = new Dialog(this,R.style.dialog);
		view_dialog = LayoutInflater.from(MainActivity.this).inflate(
				R.layout.dialog_layout, null);
		dialog.setContentView(view_dialog);
		TextView tv_cancle = (TextView) view_dialog
				.findViewById(R.id.tv_cancle);
		tv_cancle.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				dialog.dismiss();
			}
		});
		/*
		 * 获取圣诞框的窗口对象及参数对象以修改对话框的布局设置, 可以直接调用getWindow(),表示获得这个Activity的Window
		 * 对象,这样这可以以同样的方式改变这个Activity的属性.
		 */
		Window dialogWindow = dialog.getWindow();
		//设置位置
		dialogWindow.setGravity(Gravity.BOTTOM);
		//设置dialog的宽高属性
		dialogWindow.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		dialog.show();
	}

xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#fff"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:gravity="bottom|center_horizontal"
        android:orientation="vertical" >

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#efefef" />

        <TextView
            android:id="@+id/tv_cancle"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center"
            android:text="取消"
            android:textSize="20sp" />
    </LinearLayout>

</LinearLayout>

R.style.dialog:

<!--dialog样式-->
    <style name="dialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowIsFloating">true</item>
        <!-- 设置未浮动窗口 -->
        <item name="android:windowFrame">@null</item>
        <!-- 设置无边框 -->
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>
        <!-- 设置无标题 -->
        <item name="android:windowBackground">@color/sc_transparent_background</item>
        <!-- 设置完全透明 -->
        <item name="android:backgroundDimEnabled">true</item>
        <!-- 设置屏幕变暗 -->
    </style>

效果图:
android popwindow仿微信右上角弹出框,dialog底部显示



MainActivity.java:

package com.yqy.yqy_popwindow;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

public class MainActivity extends Activity {

	private TextView tv;
	private View view_pop, view_dialog;
	private PopupWindow mPopupwinow = null;
	private Button btn_dialog;
	private Dialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		/** 弹popwindow **/
		tv = (TextView) findViewById(R.id.textView1);
		view_pop = LayoutInflater.from(MainActivity.this).inflate(
				R.layout.pop_menu, null);
		tv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (mPopupwinow == null) {
					//新建一个popwindow
					mPopupwinow = new PopupWindow(view_pop,
							LayoutParams.WRAP_CONTENT,
							LayoutParams.WRAP_CONTENT, true);
					//设置popwindow的背景颜色
					mPopupwinow.setBackgroundDrawable(new ColorDrawable(
							0x00000000));
				}
				//设置popwindow的位置  tv:为微信右上角+号view,居于+号下方
				mPopupwinow.showAsDropDown(tv, 0, 0);
			}
		});

		btn_dialog = (Button) findViewById(R.id.button1);
		btn_dialog.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				showDialog();
			}
		});

	}
	
	/** 弹dialog **/
	private void showDialog() {
		dialog = new Dialog(this,R.style.dialog);
		view_dialog = LayoutInflater.from(MainActivity.this).inflate(
				R.layout.dialog_layout, null);
		dialog.setContentView(view_dialog);
		TextView tv_cancle = (TextView) view_dialog
				.findViewById(R.id.tv_cancle);
		tv_cancle.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				dialog.dismiss();
			}
		});
		/*
		 * 获取圣诞框的窗口对象及参数对象以修改对话框的布局设置, 可以直接调用getWindow(),表示获得这个Activity的Window
		 * 对象,这样这可以以同样的方式改变这个Activity的属性.
		 */
		Window dialogWindow = dialog.getWindow();
		//设置位置
		dialogWindow.setGravity(Gravity.BOTTOM);
		//设置dialog的宽高属性
		dialogWindow.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		dialog.show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}


android popwindow仿微信右上角弹出框,dialog底部显示

上一篇:微信oauth2授权获得用户信息


下一篇:让微信二维码扫描你的APK