天天看點

自定義對話框的總結

提供了三種類型的對話框,主要是将網上的資源整合,自己寫的一個demo

第一種是帶有YES/NO ,有标題,有内容的。适合讓使用者選擇是否的。一種是隻有YES的,适合通知使用者消息的。還有一種是透明簡單的對話框,也是适合通知使用者消息的。

優點:高内聚,低耦合。直接做成了幫助類,隻要把com.Synthia.dialog放入自己的工程中,調用的時候直接類名,傳參數就能調用了。簡單友善!

※YES|NO對話框 和隻有YES的對話框(選擇是否 和通知的消息對話框)

自定義對話框的總結
自定義對話框的總結

①拷貝CustomerDialog檔案及Base檔案後,在工程中的res/Valuses/Styles檔案    寫:

<style

name="Dialog"

parent="android:style/Theme.Dialog">

<item

name="android:windowNoTitle">true</item>

<item

name="android:windowIsFloating">true</item>

<item

name="android:windowFrame">@null</item>

<item

name="android:windowSoftInputMode">adjustPan</item>

</style>

<style

name="DialogText">

<item

name="android:textColor">@color/Grayish</item>

<item

name="android:textSize">15sp</item>

</style>

<style

name="DialogText.Title">

<item

name="android:textSize">21sp</item>

<item

name="android:textStyle">bold</item>

</style>
           

②在工程中res/layout的檔案寫布局檔案:kc_myself_dialog.xml。

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/dialog_title"
            style="@style/DialogText.Title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/dialog_title"
            android:gravity="center"
            android:textColor="#ffffff" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/content"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <ScrollView
            android:id="@+id/scrollView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/message"
                style="@style/DialogText"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/dialog_bottom"
                android:gravity="center_vertical|left"
                android:paddingLeft="20dp"
                android:paddingRight="20dp" />
        </ScrollView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/positiveButton"
            android:layout_width="0dip"
            android:layout_height="40dp"
            android:layout_marginTop="3dip"
            android:layout_weight="1"
            android:background="@drawable/kc_diglog_button"
            android:singleLine="true"
            android:textSize="18dp" />

        <Button
            android:id="@+id/negativeButton"
            android:layout_width="0dip"
            android:layout_height="40dp"
            android:layout_marginTop="3dip"
            android:layout_weight="1"
            android:background="@drawable/kc_diglog_button"
            android:singleLine="true"
            android:textSize="18dp" />
    </LinearLayout>

</LinearLayout>
           

③在res下建立drawable檔案夾,在其中複制kc_diglog_button.xml檔案

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Non focused states -->
    <item android:drawable="@drawable/dialog_button" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>
    <item android:drawable="@drawable/dialog_button" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>

    <!-- Focused states -->
    <item android:drawable="@drawable/dialog_button_force" android:state_focused="true" android:state_pressed="false" android:state_selected="false"/>
    <item android:drawable="@drawable/dialog_button_force" android:state_focused="true" android:state_pressed="false" android:state_selected="true"/>

    <!-- Pressed -->
    <item android:drawable="@drawable/dialog_button_force" android:state_pressed="true"/>

</selector>
           

④将四張圖檔 dialog_button.9.png、 dialog_button_force.9.png、dialog_bottom.9.png 、dialog_title.9.png和 放入檔案夾。

這四張圖檔最好是.9圖。我的工程中會給出。若不下載下傳,也可以自己照圖檔代替。

※透明的對話框

自定義對話框的總結

①在res/layout裡複制一個transparent_layout.xml檔案。

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/bgDialog"
    android:layout_width="240dp"
    android:layout_height="wrap_content"
    android:background="#0000"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical" >

        <ImageView
            android:id="@+id/ImageView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/messagebox_warning" />

        <TextView
            android:id="@+id/title01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="自定義對話框"
            android:textColor="#ffe9feff"
            android:textSize="28px" />
    </LinearLayout>

    <ScrollView
        android:id="@+id/ScrollView01"
        android:layout_width="wrap_content"
        android:layout_height="140px"
        android:background="#0000" >

        <TextView
            android:id="@+id/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#0000"
            android:textColor="#ffe9feff" >
        </TextView>
    </ScrollView>
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
	<Button
	    android:id="@+id/transparent_main_cancel"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:background="@drawable/bn9"
		android:layout_gravity="center_horizontal"
	    android:text="Cancel" />
</LinearLayout>
</LinearLayout>
           

② 在res/values/style定義對話框的樣式:

<style name="dialog" parent="@android:style/Theme.Dialog">  
        <!-- 更換背景圖檔實作全透明 -->  
        <item name="android:windowBackground">@drawable/alert_light</item>  
        <!-- 螢幕背景不變暗 -->  
        <item name="android:backgroundDimEnabled">false</item>  
        <!-- 隐藏标題 -->  
        <item name="android:windowNoTitle">true</item>  
    </style> 
           

demo下載下傳連結:

點選打開連結