Android project combat (7): Dialog theme Activity realizes custom dialog effect

Original: Android project combat (7): Dialog theme Activity realizes custom dialog effect

Everyone must have used the Activity of the Dialog theme. It is definitely a very good choice to use it to display the custom dialog effect.

That is, the interactive interface of the activity is displayed in the form of a Dialog, and the size of the Activity of the Dialog theme will be determined by the width and height of the content.

<activity android:name=MainActivity
android:theme
=”@android:style/Theme.Dialog”>
</activity>

You can see the display effect of the activity set to the Theme.Dialog theme,

It is displayed in the form of a dialog box, and the background is the previous activity interaction interface of this Activity.

Or if this Activity is the first Activity of the program, the background is the mobile phone desktop

So let's make a pretty dialog-style Activity ourselves

First, remove the title that comes with the Activity

Use the requestWindowFeature(Window.FEATURE_NO_TITLE); statement

Note that it needs to be used before the setContentView(R.layout.main); statement

Draw a layout, the specific effect depends on how your Xml is written

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        android:orientation="vertical"
        android:background="@drawable/duanxinbeijing"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="0.0dip"
            android:layout_weight="2.0">
        <TextView
                android:layout_gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="15.0dip"
                android:layout_marginBottom="15.0dip"
                android:layout_weight="1.0" />
    </LinearLayout>
    <TextView
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="15.0dip"
            android:text="是否拨打电话"
            android:textSize="20sp"
            android:layout_weight="1.0" />
    <View
            android:background="#ffd1d1d1"
            android:layout_width="fill_parent"
            android:layout_height="2.0px" />
    <LinearLayout
            android:layout_gravity="center"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
        <ImageButton
                android:id="@+id/call_dialog_queren"
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_marginTop="10.0dip"
                android:layout_marginBottom="10.0dip"
                android:src="@drawable/dialogqueren"
                android:background="#0000"
                android:layout_weight="1.0" />
        <ImageButton
                android:id="@+id/call_dialog_quxiao"
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_marginTop="10.0dip"
                android:layout_marginBottom="10.0dip"
                android:src="@drawable/dialogquxiao"
                android:background="#0000"
                android:layout_weight="1.0" />
    </LinearLayout>
</LinearLayout>
View Code

Activity key code:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE); //Remove the title bar of this Activity
        setContentView(R.layout.main);
    }

See the renderings:

------------------------------------------------------------------------------------------------

At present, the above is the operation that we all generally use, but if we need a more beautiful and better user experience, such as a rounded dialog box, and the above method can clearly see that when the background is a rounded image , the effect of the four corners is very poor. android:theme=”@android:style/Theme.Dialog” The theme of the Activity is a boxy dialog style.

The implementation method is to customize a style, in the res/styles.xml file

<style name="MyDialogStyle">
        <item name= " android:windowBackground " >@android:color/transparent</item> Set the background of the dialog, here is the transparent value given by the system
        <item name= " android:windowFrame " >@null</item> Dialog's windowFrame frame is none
        <item name= " android:windowNoTitle " > true </item> Whether to display the title
        <item name= " android:windowIsFloating " > true </item> Whether to float above the activity
        <item name= " android:windowIsTranslucent " > true </item> Whether it is translucent
        <item name= " android:windowContentOverlay " >@null</item> Whether there is an overlay
        <item name= " android:windowAnimationStyle " >@android:style/Animation.Dialog</item> Set how Activity appears
        <item name= " android:backgroundDimEnabled " > true </item> Whether the background is blurred
</style>

The layout file is unchanged, and then change the manifest configuration file:

<activity
     android:name = " MainActivity " 
     android:theme = " @style/MyDialogStyle " /> //The theme is set to our custom style
<activity

You can see the effect, are the four right angles become the rounded corners corresponding to the background image?

 

Of course, it is still an Activity, so you can operate the controls in the Activity as usual

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325034103&siteId=291194637