Custom Dialog width occupies full screen and dialog width

Source: Internet
Author: User

Custom Dialog width occupies full screen and dialog width
1. Custom Dialog inherits the Dialog

public class MyDialog extends Dialog {

 

2. Set a style for Dialog

Create new style inheritance in style

@ Android: style/Theme. Dialog or @ android: style/Theme. Holo. Dialog
  • Set style to remove border
  • Remove title
  • Set window transparency
  • The Setting Dialog Box can disappear.
  • Set Animation

 

<! -- <Style name = "MyDialog" parent = "@ android: style/Theme. dialog "> --> <style name =" MyDialog "parent =" @ android: style/Theme. holo. dialog "> <! -- Whether a Border exists --> <item name = "android: windowFrame"> @ null </item> <! -- Whether it is above the suspended Activity --> <item name = "android: Refreshing wisfloating"> true </item> <! -- Title --> <item name = "android: windowNoTitle"> true </item> <! -- Shadow --> <item name = "android: javaswistranslucent"> true </item> <! -- Translucent --> <! -- Enter and exit the animation --> <item name = "android: windowAnimationStyle"> @ style/MyDialogAnimation </item> <! -- Disappears from the outside --> <item name = "android: windowCloseOnTouchOutside"> true </item> </style> <style name = "MyDialogAnimation"> <! -- Enter --> <item name = "android: windowEnterAnimation"> @ anim/dialog_enter </item> <! -- Exit --> <item name = "android: javaswexitanimation"> @ anim/dialog_exit </item> </style>

 

Enter Animation

dialog_enter
dialog_exit
The built-in system can be found and used directly in the SDK.
Directory \ sdk \ platforms \ corresponding API version

<set xmlns:android="http://schemas.android.com/apk/res/android"        android:shareInterpolator="false" >    <scale android:fromXScale="0.9" android:toXScale="1.0"           android:fromYScale="0.9" android:toYScale="1.0"           android:pivotX="50%" android:pivotY="50%"                   android:duration="200" />    <alpha android:fromAlpha="0.0" android:toAlpha="1.0"            android:duration="200" />   </set>

Exit

<set xmlns:android="http://schemas.android.com/apk/res/android"     android:shareInterpolator="false">    <scale        android:duration="200"        android:fromXScale="1.0"        android:fromYScale="1.0"        android:pivotX="50%"        android:pivotY="50%"        android:toXScale="0.9"        android:toYScale="0.9"/>    <alpha        android:duration="200"        android:fromAlpha="1.0"        android:toAlpha="0.0"/></set>
3. Set the style in the constructor
Context mContext;    public MyDialog(Context context) {        super(context, R.style.MyDialog);        this.mContext=context;            }
Iv. Set Layout
@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.layout_dialog);    }

The layout file contains two textviews.

5. Rewrite the show method and set the width and height.
@ Override public void show () {super. show ();/*** set the width to full screen. You must set the width to the back of show */LayoutParams layoutParams = getWindow (). getAttributes (); layoutParams. gravity = Gravity. BOTTOM; layoutParams. width = LayoutParams. MATCH_PARENT; layoutParams. height = LayoutParams. WRAP_CONTENT; getWindow (). getDecorView (). setPadding (0, 0, 0, 0); getWindow (). setAttributes (layoutParams );}

 

Vi. Complete Dialog class
/*** Dialog July 30, 2016 */public class MyDialog extends Dialog {Context mContext; public MyDialog (Context context) {super (context, R. style. myDialog); this. mContext = context;} @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. layout_dialog) ;}@ Override public void show () {super. show ();/*** set the width to full screen. You must set the width to the back of show */LayoutParams layoutParams = getWindow (). getAttributes (); layoutParams. gravity = Gravity. BOTTOM; layoutParams. width = LayoutParams. MATCH_PARENT; layoutParams. height = LayoutParams. WRAP_CONTENT; getWindow (). getDecorView (). setPadding (0, 0, 0, 0); getWindow (). setAttributes (layoutParams );}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.