gpt4 book ai didi

Android使用setCustomTitle()方法自定义对话框标题

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Android使用setCustomTitle()方法自定义对话框标题由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

android有自带的对话框标题,但是不太美观,如果要给弹出的对话框设置一个自定义的标题,使用alertdialog.builder的setcustomtitle()方法.

运行效果如下,左边是点击第一个按钮,弹出android系统自带的对话框(直接用settitle()设置标题);右边是点击第二个按钮,首先inflate一个view,然后用setcustomtitle()方法把该view设置成对话框的标题.

Android使用setCustomTitle()方法自定义对话框标题Android使用setCustomTitle()方法自定义对话框标题

定义一个对话框标题的title.xml文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?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的布局文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<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文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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使用setcustomtitle()方法自定义对话框标题,希望对大家有所帮助.

最后此篇关于Android使用setCustomTitle()方法自定义对话框标题的文章就讲到这里了,如果你想了解更多关于Android使用setCustomTitle()方法自定义对话框标题的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com