Android bottom of the pop-realization

Article: https://www.jianshu.com/p/366aaa3efa2a  (Translate) to explain

          https://blog.csdn.net/zhangqunshuai/article/details/80858219

1, the effect of FIG.

 

 

 

achieve

1, the bottom of the pop layout (dialog.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="#fff"
android:orientation="vertical">

<TextView
android:id="@+id/tv_take_photo"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="拍摄"
android:textColor="@android:color/background_dark"
android:textSize="16sp" />

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray" />

<TextView
android:id="@+id/tv_take_pic"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="相册选择"
android:textColor="@android:color/background_dark"
android:textSize="16sp" />

<View
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="@android:color/darker_gray" />

<TextView
android:id="@+id/tv_cancel"
android:layout_width="match_parent"
android:layout_height="50dp"
Android: Gravity = "Center"
Android: text = "Cancel"
Android: textColor = "@ Android: Color / background_dark"
Android: textSize = "16SP" />

</ LinearLayout>

2, set the pop-up, exit the movie

I, in res New resource file directory file anim
Second, the new layout in the anim file

1, comes into play animation
    dialog_in_anim.xml
<? XML Version = "1.0" encoding = "UTF-. 8"?> 
<SET xmlns: Android = "http://schemas.android.com/apk/res/android">
<Translate
Android: DURATION = "300" // animation execution time
Android: fromXDelta = "0"
Android: fromYDelta = "1000" from the bottom of the pop-up //
Android: toXDelta = "0"
Android: toYDelta = "0" />

</ SET>
  1, exit animation 
   dialog_out_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="300"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="0"
android:toYDelta="1000"//回到屏幕外
/>

</set>
3、style 样式
<style name="DialogTheme" parent="@android:style/Theme.Dialog">
<!-- 边框 -->
<item name="android:windowFrame">@null</item>
<!-- 是否浮现在activity之上 -->
<item name="android:windowIsFloating">true</item>
<!-- 半透明 -->
<item name="android:windowIsTranslucent">true</item>
<!-- 无标题 -->
<item name="android:windowNoTitle">true</item>
<item name="android:background">@android:color/transparent</item>
<!-- 背景透明 -->
<item name="android:windowBackground">@android:color/transparent</item>
<!-- 模糊 -->
<item name="android:backgroundDimEnabled">true</item>
<!-- 遮罩层 -->
<item name="android:backgroundDimAmount">0.5</item>
</style>

<!-- dialog的动画 -->
<style name="main_menu_animStyle">
<item name="android:windowEnterAnimation">@anim/dialog_in_anim</item>
<item name="android:windowExitAnimation">@anim/dialog_out_anim</item>
</style>

Implementation

 mainActivity.xml:

void showBottomDialog Private () { 
//. 1, use Dialog, set the style
Final = new new Dialog Dialog Dialog (the this, R.style.DialogTheme);
// 2, set the layout
View view = View.inflate (this, R.layout . Dialog, null);
dialog.setContentView (View);

the window window dialog.getWindow = ();
// set the eject position
window.setGravity (Gravity.BOTTOM);
// set the animation pop
window.setWindowAnimations (R.style.main_menu_animStyle) ;
// set the size of the dialog
window.setLayout (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
with dialog.show ();

dialog.findViewById (R.id.tv_take_photo) .setOnClickListener (new new View.OnClickListener () {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});

dialog.findViewById(R.id.tv_take_pic).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});

dialog.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});

}
完成




也可以封装成方法:
showBottomDialog.class:
{class showBottomDialog public 
Private View View;
public void BottomDialog (the Context context) {
//. 1, use Dialog, set the style
Final new new Dialog Dialog Dialog = (context, R.style.DialogTheme);
// 2, set the layout
view = View .inflate (context, R.layout.dialog, null);
dialog.setContentView (View);

the window window dialog.getWindow = ();
// set the eject position
window.setGravity (Gravity.BOTTOM);
// set pop animation
window .setWindowAnimations (R.style.main_menu_animStyle);
// set the size of the dialog
window.setLayout (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
with dialog.show ();

dialog.findViewById(R.id.tv_take_photo).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});

dialog.findViewById(R.id.tv_take_pic).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});

dialog.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
}
}

Using

 

Guess you like

Origin www.cnblogs.com/guochangxin/p/11457537.html