安卓如何模仿ios,可以显示如图的有遮盖效果的界面?

ios可以在window基础上做,刚学安卓,想知道安卓是否有?求大神指点怎么实现这样的效果?

我自定义一个view,继承Dialog,没有这样的遮罩效果,源代码:

为什么上面会有Dialog的标题栏?

Dialog即可实现你想要的效果,设置Dialog的window属性,可以改变dialog在屏幕的显示位置。如,你截图的效果可以这么写

Dialog mDialog = new Dialogthis;
Window mWindow = mDialog.getWindow;
mWindow.setLayoutLinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT;
mWindow.setGravityGravity.BOTTOM;

当然,dialog里的布局内容就需要你再去放置了。

supportlibrary 23.2.0 ,提供了BottomSheetDialogFragment ,轻松达到想得到的效果

就是一个dialog

PhotoChioceDialog

public class PhotoChioceDialog { private Context context; private Dialog dialog; private ClickListenerInterface clickListenerInterface; public PhotoChioceDialogContext context{ this.context =context; dialog = new Dialogcontext, R.style.photo_chioce_dialog; dialog.setContentViewR.layout.dialog_pic_chioce; dialog.findViewByIdR.id.btn_album.setOnClickListenernew ClickListener; dialog.findViewByIdR.id.btn_camera.setOnClickListenernew ClickListener; dialog.findViewByIdR.id.btn_cancel.setOnClickListenernew ClickListener; Window window = dialog.getWindow; window.setWindowAnimationsR.style.main_menu_animstyle; WindowManager.LayoutParams lp = window.getAttributes; WindowManager manager=WindowManager context.getSystemServiceContext.WINDOW_SERVICE; lp.x = 0; lp.y = manager.getDefaultDisplay.getHeight; // 以下这两句是为了保证按钮可以水平满屏 lp.width = ViewGroup.LayoutParams.MATCH_PARENT; lp.height = ViewGroup.LayoutParams.WRAP_CONTENT; // 设置显示位置 dialog.onWindowAttributesChangedlp; // 设置点击外围解散 dialog.setCanceledOnTouchOutsidetrue; } public void show{ dialog.show; } public void dismiss{ dialog.dismiss; } public void setClickListenerInterfaceClickListenerInterface clickListenerInterface { this.clickListenerInterface = clickListenerInterface; } public interface ClickListenerInterface { /** * 进入相册 */ void doAlbum; /** * 取消 */ void doCancel; /** * 进入相机 */ void doCamera; } private class ClickListener implements View.OnClickListener{ @Override public void onClickView v { switch v.getId { case R.id.btn_album: if clickListenerInterface!=null clickListenerInterface.doAlbum; break; case R.id.btn_camera: if clickListenerInterface!=null clickListenerInterface.doCamera; break; case R.id.btn_cancel: if clickListenerInterface!=null clickListenerInterface.doCancel; break; } } }
}

R.style.photo_chioce_dialog

<style name="photo_chioce_dialog" parent="@android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:windowIsTranslucent">false</item> <item name="android:windowNoTitle">true</item> <item name="android:background">@color/transparent</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:backgroundDimEnabled">true</item> </style>

dialog_pic_chioce

<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:layout_margin="10dp" > <Button android:id="@+id/btn_album" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/photo_album_selector" android:text="@string/album" android:textColor="@color/black" android:textSize="18sp" android:padding="10dp"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/line"/> <Button android:id="@+id/btn_camera" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/photo_camera_selector" android:text="@string/camera" android:textColor="@color/black" android:textSize="18sp"/> <Button android:id="@+id/btn_cancel" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/photo_cancel_selector" android:paddingBottom="16dp" android:layout_marginTop="10dp" android:text="@string/cancel" android:textColor="@color/black" android:textSize="18sp"/>
</LinearLayout>

动画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>

动画

photo_dialog_in_anim
<set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="500" android:fromXDelta="0" android:fromYDelta="1000" android:toXDelta="0" android:toYDelta="0" /> </set> photo_dialog_out_anim
<set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="500" android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="0" android:toYDelta="1000" /> </set>

发表评论

电子邮件地址不会被公开。 必填项已用*标注