看杭电APP之头像选择弹出对话框(Dialog)

 
 
	View view = getLayoutInflater().inflate(
						R.layout.photo_choose_dialog, null);
				final Dialog dialog = new Dialog(MainActivity1.this,
						R.style.transparentFrameWindowStyle);
				//为view中控件添加点击相应
				Button btnCancle = (Button) view
						.findViewById(R.id.btn_photochoose_cancle);

				// 传入界面
				dialog.setContentView(view, new LayoutParams(
						LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
				Window window = dialog.getWindow();
				// 设置显示动画
				window.setWindowAnimations(R.style.main_menu_animstyle);
				WindowManager.LayoutParams wl = window.getAttributes();//得到分层窗口
				wl.x = 0;
				wl.y = getWindowManager().getDefaultDisplay().getHeight();
				// 以下这两句是为了保证按钮可以水平满屏
				wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
				wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;

				// 设置显示位置
				dialog.onWindowAttributesChanged(wl);
				// 设置点击外围解散
				dialog.setCanceledOnTouchOutside(true);
				dialog.show();
				


R.style文件:

  <!-- 头像选择对话框 -->
    <style name="transparentFrameWindowStyle" parent="android:style/Theme.Dialog">
        <item name="android:windowBackground">@drawable/photo_choose_bg</item>
    </style>

    <style name="main_menu_animstyle">
        <item name="android:windowEnterAnimation">@anim/photo_dialog_in_anim</item>
        <item name="android:windowExitAnimation">@anim/photo_dialog_out_anim</item>
    </style>

anim_in.xml:
    <translate
        android:duration="500"
        android:fromXDelta="0"
        android:fromYDelta="1000"
        android:toXDelta="0"
        android:toYDelta="0" />


anim_out.xml:

 <translate
        android:duration="500"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="0"
        android:toYDelta="1000" />

layout.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="match_parent"
    android:background="#00000000"
    android:gravity="bottom"
    android:orientation="vertical"
    android:padding="5dip" >

    <Button
        android:id="@+id/btn_photochoose_photobox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/photo_gallery_selector"
        android:paddingBottom="10dip"
        android:paddingTop="10dip"
        android:text="图库"
        android:textSize="16sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0.5dip"
        android:background="#DAD9DB" />

    <Button
        android:id="@+id/btn_photochoose_take"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/photo_camera_selector"
        android:paddingBottom="10dip"
        android:paddingTop="10dip"
        android:text="拍照"
        android:textSize="16sp" />

    <Button
        android:id="@+id/btn_photochoose_cancle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip"
        android:background="@drawable/photo_cancel_selector"
        android:paddingBottom="10dip"
        android:paddingTop="10dip"
        android:text="取消"
        android:textSize="16sp" />

</LinearLayout>

相关drawable可自行设置

本文主要介绍了dialog与view、layoutinflat相结合使用的知识以及核心的几个代码组成,由于此例只涉及UI的绘制以及少数相关功能故无法一一列出dialog,如需请自行查阅相关API文档


看杭电APP之头像选择弹出对话框(Dialog)_第1张图片

你可能感兴趣的:(java,android,APP,UI设计)