Custom title of the Android dialog box and the title of the android dialog box

Source: Internet
Author: User

Custom title of the Android dialog box and the title of the android dialog box

The title of the dialog box that comes with Android is not easy to understand. If you need to set a custom title for the pop-up dialog box, you can use the setCustomTitle () method of AlertDialog. Builder.

Define the title. xml file of a dialog box title:

<? 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 = "select city" android: textColor = "@ color/white" android: textSize = "20sp"/> </LinearLayout>

MainActivity layout file:

<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/Hangzhou" 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 = "select city 1"/> <Button android: id = "@ + id/btn02" android: layout_width = "150dp" android: layout_height = "wrap_content" android: text = "select city 2"/> </LinearLayout>

MainActivity. java file:

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] = "Beijing"; cities [1] = "Shanghai "; cities [2] = "Shenzhen"; cities [3] = "Guangzhou"; cities [4] = "Hangzhou"; cities [5] = "Chengdu "; 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 ("select city"); 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 ;}}}

The running effect is as follows. Click the first button on the left to bring up a dialog box that comes with the Android system (set the title directly using setTitle (). Click the second button on the right to inflate a View first, then, use setCustomTitle () to set the View as the title of the dialog box.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.