对话框样式的activity实现与美化


转自:http://www.apkbus.com/thread-166567-1-1.html?fromuid=321014

很多时候,我们需要在桌面或者其他地方(当前view不是Activity的情况)弹出一个提示对话框,例如闹钟提示,系统错误提示等,如果直接用dialog实现这样的对话框就会报错,原因是dialog对话框在实例化时必须有一个Context类型参数,即对话框的弹出式和当前activity相联系起来的,这种办法行不通。既然直接用dialog不行,那么我们就用Activity实现吧,不过这个Activity看起来和dialog一样。具体实现如下:

先定义一个布局文件dialog.xm:

<?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="#ffffff"
android:orientation="vertical" >

<TextView
android:id="@+id/dialog_tv"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:textSize="17dp"
android:textColor="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<Button
android:id="@+id/b1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/select_text"
/>

<Button
android:id="@+id/b2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/select_text"
/>
</LinearLayout>

</LinearLayout>
然后在清单文件里面定义Activity的样式:

<activity
android:name="com.example.sidebar2.dialog"
android:theme="@android:style/Theme.Dialog" >
</activity>
接下来在dialog类中加载视图就可以了,具体效果如下:


但是感觉这样的窗口太丑了,一点都不美观,没办法,系统提供的东西就这样。想要好看点就自己动手吧。我们先在drawable文件夹下新建一个filled_box.xml:

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

<solid android:color="#dd0a0000" />

<stroke
android:width="3dp"
android:color="#980a0000" />

<corners android:radius="20dp" />

<padding
android:bottom="10dp"
android:left="5dp"
android:right="5dp"
android:top="10dp" />

</shape>

这里,
solid 也就是实体话  填充色的意思 就是整个背景的颜色
stroke 就是描边的意思 也就是边缘的颜色如果设置不仔细是没有效果的 有点要求这个
corners就是圆角的实现。
然后,在values文件夹下的styles.xml文件中加入如下代码

<style name="Theme" parent="android:Theme"></style>

<style name="Theme.CustomDialog" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">@drawable/filled_box</item>
<item name="android:windowNoTitle">true</item>
</style>
下一步就是新建一个布局文件dialog2.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<ImageView
android:layout_width="35dp"
android:layout_height="40dp"
android:layout_margin="5dp"
android:src="@drawable/gantan" />

<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:gravity="center_vertical"
android:text="温馨提示"
android:textColor="#ffffff"
android:textSize="20sp" />
</LinearLayout>

<TextView
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="5dp"
android:background="#838186" />

<TextView
android:id="@+id/dialogtv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:gravity="center"
android:textColor="#838186"
android:textSize="25sp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal" >

<Button
android:id="@+id/dialog_button1"
android:layout_marginLeft="8dp"
android:layout_width="140dp"
android:text="确认"
android:textSize="17sp"
android:layout_height="match_parent"
android:background="@drawable/select_text" />

<Button
android:id="@+id/dialog_button2"
android:layout_marginLeft="5dp"
android:textSize="17sp"
android:layout_width="140dp"
android:text="取消"
android:layout_height="match_parent"
android:background="@drawable/select_text2" />
</LinearLayout>

</LinearLayout>

其中的select_text.xml文件是使按钮有按下的感觉,就是按下颜色变化

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/queren1" android:state_pressed="false"/>
<item android:drawable="@drawable/queren2" android:state_pressed="true"/>
<item android:drawable="@drawable/queren2" android:state_focused="true"/>
<item android:drawable="@drawable/queren1"/>

</selector>

最后就在清单文件中声明自己定义的样式就可以了

<activity
android:name="com.example.sidebar2.dialog"
android:theme="@style/Theme.CustomDialog" >
</activity>

具体结果如下怎么样,比系统自带的漂亮多了吧。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值