Customize dialog box plus status selector

Source: Internet
Author: User

Write a demo to review the custom dialog box and state selector

First up effect

Source: http://download.csdn.net/detail/qq_35463672/9591065

In the first step, two pictures are placed under drawable-hdpi, and in res folder drawable the state selector in which Img.xml is created

<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android ">    <item android:drawable=" @drawable/fire "android:state_pressed=" true "/> <!--pressed-- >    <item android:drawable= "@drawable/fire" android:state_focused= "true"/> <!--focused    -- default --></selector>

Three states for different pictures

Step two: Create Mydialog to try to

<?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:orientation= "Vertical" > <TextView android:gravity= "Center"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Small Flame"/> <!--Be sure to add this sentence android:clickable= "true"-<ImageView android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:clickable= "true"Android:background= "@drawable/img"/></linearlayout>

Note that you must add this android:clickable= "true" to respond to a click and the state selector is valid

Step Three: Write Mainactivity

 Packagecom.example.mytest002;Importandroid.app.Activity;ImportAndroid.app.AlertDialog;ImportAndroid.app.AlertDialog.Builder;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button; Public classMainactivityextendsActivity {Button button1; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Button1=(Button) Findviewbyid (R.id.button1); Button1.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {ShowDialog ();    }        }); }     Public voidShowDialog () {Alertdialog.builder Builder=NewBuilder ( This); Alertdialog Dialog=builder.create (); View View=view.inflate ( This, R.layout.my_dialog,NULL); //remove Upper and lower left and right margins, compatible with 2.x versionDialog.setview (View, 0,0,0,0);    Dialog.show (); }  }

Customize dialog box plus status selector

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.