Android对话框自定义标题 对话框标题美化操作
程序员文章站
2024-03-07 09:11:14
android自带的对话框标题不好看,如果我们需要给弹出的对话框设置一个自己定义的标题,可以使用alertdialog.builder的setcustomtitle()方法...
android自带的对话框标题不好看,如果我们需要给弹出的对话框设置一个自己定义的标题,可以使用alertdialog.builder的setcustomtitle()方法。
定义一个对话框标题的title.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:background="@color/white" android:gravity="center_vertical" android:orientation="vertical" > <linearlayout android:id="@+id/patient_top" android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignparenttop="true" android:background="@color/green" android:gravity="center_vertical|center_horizontal" android:orientation="vertical" > <textview android:id="@+id/txtpatient" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选择城市" android:textcolor="@color/white" android:textsize="20sp" /> </linearlayout> </linearlayout>
mainactivity的布局文件:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical|center_horizontal" android:orientation="vertical" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.hzhi.dialogtest.mainactivity" > <button android:id="@+id/btn01" android:layout_width="150dp" android:layout_height="wrap_content" android:text="选择城市1" /> <button android:id="@+id/btn02" android:layout_width="150dp" android:layout_height="wrap_content" android:text="选择城市2" /> </linearlayout>
mainactivity.java文件:
package com.hzhi.dialogtest; import android.support.v7.app.actionbaractivity; import android.app.alertdialog; import android.content.dialoginterface; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; public class mainactivity extends actionbaractivity implements onclicklistener{ final string[] cities = new string[6]; button button_01, button_02; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initview(); } private void initview(){ cities[0] = "北京"; cities[1] = "上海"; cities[2] = "深圳"; cities[3] = "广州"; cities[4] = "杭州"; cities[5] = "成都"; button_01 = (button) findviewbyid(r.id.btn01); button_01.setonclicklistener(this); button_02 = (button) findviewbyid(r.id.btn02); button_02.setonclicklistener(this); } @override public void onclick(view v) { // todo auto-generated method stub switch (v.getid()){ case r.id.btn01: alertdialog.builder builder1 = new alertdialog.builder(mainactivity.this); builder1.setitems(cities, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { } }); builder1.settitle("选择城市"); builder1.show(); break; case r.id.btn02: layoutinflater layoutinflater = layoutinflater.from(mainactivity.this); view mtitleview = layoutinflater.inflate(r.layout.title, null); alertdialog.builder builder2 = new alertdialog.builder(mainactivity.this); builder2.setitems(cities, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { } }); builder2.setcustomtitle(mtitleview); builder2.show(); break; } } }
运行效果如下,左边是点击第一个按钮,弹出android系统自带的对话框(直接用settitle()设置标题);右边是点击第二个按钮,首先inflate一个view,然后用setcustomtitle()方法把该view设置成对话框的标题。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。