Android dialog box, advanced settings, "1" Setting dialog box button, and insert an image into the content text

Source: Internet
Author: User

We know that the style of the dialog box provided by the Android system is a little simple. If your application is gorgeous and coupled with a simple dialog box, it seems out of place.

So ...... we can use reflection technology to prevent the close of the dialog box, set the transparency of the dialog box, set the button image, and so on.

Here we set the image of the button in the dialog box and insert the image into the content text. Need to combine HTML language ..

The running result is:

Source code:

Layout XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    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=".MainActivity" >    <Button        android:onClick="onclick_button"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/hello_world" /></RelativeLayout>

Java source code:

Package COM. example. dialog_image_text; import android. OS. bundle; import android. app. activity; import android. app. alertdialog; import android. graphics. drawable. drawable; import android. text. HTML; import android. text. HTML. imagegetter; import android. view. menu; import android. view. view; public class mainactivity extends activity {@ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main) ;}@ overridepublic Boolean oncreateoptionsmenu (menu) {// inflate the menu; this adds items to the action bar if it is present. getmenuinflater (). inflate (R. menu. main, menu); Return true;} public void onclick_button (view v) {New alertdialog. builder (this ). settitle ("insert image and set button image in dialog box text "). setmessage (HTML. fromhtml ("Haha,  Hi ", new imagegetter () {@ overridepublic drawable getdrawable (string source) {// todo auto-generated method stubdrawable mdrawable = getresources (). getdrawable (R. drawable. face); mdrawable. setbounds (0, 0, 32, 32); Return mdrawable ;}}, null )). setpositivebutton (HTML. fromhtml (" OK ", new imagegetter () {public drawable getdrawable (string source) {// todo auto-generated method stubdrawable mdrawable = getresources (). getdrawable (R. drawable. OK); mdrawable. setbounds (0, 0, 20, 20); Return mdrawable ;}}, null), null ). setnegativebutton (HTML. fromhtml (" cancel ", new imagegetter () {@ overridepublic drawable getdrawable (string source) {// todo auto-generated method stubdrawable mdrawable = getresources (). getdrawable (R. drawable. cancel); mdrawable. setbounds (0, 0, 20, 20); Return mdrawable ;}}, null), null ). show ();}}

OK.

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.