安卓底部弹窗背景透明(使用Activity实现)

实际开发中会经常碰到这样的需求,从底部弹出一个类似对话框的UI,很多分享的界面就是类似这种UI。

实现原理大概有2种方式:第一种就是使用Activity实现,第二种就是利用PopWindow,这边文章说下使用Activity的实现方法。


Activity的实现原理:

1.弹框的Activity中定义一个布局(高度要采用包裹内容的方式,否则占满全屏不会有透明效果)

alert_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/pop_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@drawable/btn_style_alert_dialog_background"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <Button
            android:id="@+id/btn_take_photo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dip"
            android:layout_marginRight="20dip"
            android:layout_marginTop="20dip"
            android:background="@drawable/btn_style_alert_dialog_button"
            android:text="拍照"
            android:textStyle="bold" />

        <Button
            android:id="@+id/btn_pick_photo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dip"
            android:layout_marginRight="20dip"
            android:layout_marginTop="5dip"
            android:background="@drawable/btn_style_alert_dialog_button"
            android:text="从相册选择"
            android:textStyle="bold" />

        <Button
            android:id="@+id/btn_cancel"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="15dip"
            android:layout_marginLeft="20dip"
            android:layout_marginRight="20dip"
            android:layout_marginTop="15dip"
            android:background="@drawable/btn_style_alert_dialog_cancel"
            android:text="取消"
            android:textColor="#ffffff"
            android:textStyle="bold" />
    </LinearLayout>

</RelativeLayout>


2.定义弹框Activity的样式(包括进入、退出时的动画)

styles.xml

<resources>
    <style name="AppTheme" parent="android:Theme.Light" />
    <!-- 定义弹框Activity的进入、关闭动画 -->
    <style name="AnimBottom" parent="@android:style/Animation">
        <item name="android:windowEnterAnimation">@anim/push_bottom_in</item>
        <item name="android:windowExitAnimation">@anim/push_bottom_out</item>
    </style>
    
    <style name="MyDialogStyleBottom" parent="android:Theme.Dialog">
        <item name="android:windowAnimationStyle">@style/AnimBottom</item>
        <item name="android:windowFrame">@null</item>
        <!-- 边框 -->
        <item name="android:windowIsFloating">false</item>
        <!-- 是否浮现在activity之上 -->
        <item name="android:windowIsTranslucent">true</item>
        <!-- 半透明 -->
        <item name="android:windowNoTitle">true</item>
        <!-- 无标题 -->
        <item name="android:windowBackground">@android:color/transparent</item>
        <!-- 背景透明 -->
        <item name="android:backgroundDimEnabled">true</item>
        <!-- 模糊 -->
    </style>
</resources>

在value下colors.xml中定义透明颜色值transparent 

<color name="transparent">#00000000</color>


3.在anim下定义Activity的进入和退出的动画

push_bottom_in.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- 上下滑入式 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="200"
        android:fromYDelta="100%p"
        android:toYDelta="0"        
     />      
</set>


push_bottom_out.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- 上下滑出式 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="200"
        android:fromYDelta="0"
        android:toYDelta="50%p" />
</set>



4.在清单文件中配置

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.picpopupwindow"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- 定义弹框的Activity,并指定其动画 -->
        <activity android:name=".SelectPicPopupWindow" android:theme="@style/MyDialogStyleBottom" /> 
    </application>

</manifest>



5.定义弹窗的Activity

SelectPicPopupWindow.java

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
public class SelectPicPopupWindow extends Activity implements OnClickListener{

	private Button btn_take_photo, btn_pick_photo, btn_cancel;
	private LinearLayout layout;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.alert_dialog);
		btn_take_photo = (Button) this.findViewById(R.id.btn_take_photo);
		btn_pick_photo = (Button) this.findViewById(R.id.btn_pick_photo);
		btn_cancel = (Button) this.findViewById(R.id.btn_cancel);
		
		layout=(LinearLayout)findViewById(R.id.pop_layout);
		
		//添加选择窗口范围监听可以优先获取触点,即不再执行onTouchEvent()函数,点击其他地方时执行onTouchEvent()函数销毁Activity
		layout.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), "提示:点击窗口外部关闭窗口!", 
						Toast.LENGTH_SHORT).show();	
			}
		});
		//添加按钮监听
		btn_cancel.setOnClickListener(this);
		btn_pick_photo.setOnClickListener(this);
		btn_take_photo.setOnClickListener(this);
	}
	
	//实现onTouchEvent触屏函数但点击屏幕时销毁本Activity
	@Override
	public boolean onTouchEvent(MotionEvent event){
		finish();//如果不想点击外部关闭弹窗去掉此行代码即可
		return true;
	}
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_take_photo:
			break;
		case R.id.btn_pick_photo:				
			break;
		case R.id.btn_cancel:				
			break;
		default:
			break;
		}
		finish();
	}
}
需要调用时只需要开启SelectPicPopupWindow即可。

效果图:


完整Demo下载:http://download.csdn.net/detail/linder_qzy/9454611


  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Activity实现一个 BottomSheetDialog 底部弹窗,可以按照以下步骤进行: 1. 创建一个继承自 BottomSheetDialogFragment 的 Fragment 类,用于显示底部弹窗内容。在该 Fragment 中重写 onCreateDialog 方法,返回一个 BottomSheetDialog 对象。 例如: ``` public class MyBottomSheetDialogFragment extends BottomSheetDialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { BottomSheetDialog dialog = new BottomSheetDialog(getActivity()); dialog.setContentView(R.layout.bottom_sheet_dialog); return dialog; } } ``` 2. 在 Activity 中创建一个实例变量,用于保存 BottomSheetDialogFragment 的实例。 例如: ``` private MyBottomSheetDialogFragment mBottomSheetDialogFragment; ``` 3. 在需要显示底部弹窗的地方,调用以下代码来显示 BottomSheetDialogFragment: ``` mBottomSheetDialogFragment = new MyBottomSheetDialogFragment(); mBottomSheetDialogFragment.show(getSupportFragmentManager(), "MyBottomSheetDialogFragment"); ``` 4. 要在 Activity 中获取 BottomSheetDialogFragment 所在的实例,可以在 onCreateDialog 方法中将 BottomSheetDialog 的实例传递给 Fragment,并在 Fragment 中保存该实例。 例如,在 MyBottomSheetDialogFragment 类中添加以下代码: ``` private BottomSheetDialog mBottomSheetDialog; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mBottomSheetDialog = (BottomSheetDialog) getDialog(); } public BottomSheetDialog getBottomSheetDialog() { return mBottomSheetDialog; } ``` 然后,在 Activity 中可以通过以下代码获取 BottomSheetDialogFragment 所在的 Fragment 实例: ``` MyBottomSheetDialogFragment fragment = (MyBottomSheetDialogFragment) getSupportFragmentManager().findFragmentByTag("MyBottomSheetDialogFragment"); BottomSheetDialog dialog = fragment.getBottomSheetDialog(); ``` 这样就可以在 Activity 中获取 BottomSheetDialog 所在的 Fragment 实例了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值