自定义对话框的总结

提供了三种类型的对话框,主要是将网上的资源整合,自己写的一个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下载链接:
点击打开链接



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值