Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android popwindow仿微信右上角彈出框,dialog底部顯示

android popwindow仿微信右上角彈出框,dialog底部顯示

編輯:關於Android編程

仿微信右上角彈出框

1、利用popwindow實現

2、popwindow的位置居於右上角

新建,彈出popwindow:

/** 彈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);
			}
		});
布局pop_menu.xml:



    

        

            

            
        

        

        

            

            
        

        

        

            

            
        

        

        

            

            
        
    




效果:
\

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:



    

        

        
    


R.style.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;
	}

}


 
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved