标签:
写个Demo复习下自定义对话框和状态选择器
先上效果
源码:http://download.csdn.net/detail/qq_35463672/9591065
第一步,两张图片放到drawable-hdpi下,在res下文件夹drawable再在其中创建img.xml的状态选择器
<?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 -->
<item android:drawable="@drawable/normal"/>
<!-- default -->
</selector>
三种状态对应不同的图片
第二步:创建mydialog试图
<?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="小型火焰"/> <!-- 一定要加上这句android:clickable="true" --> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:clickable="true" android:background="@drawable/img"/> </LinearLayout>
注意,一定要加上这句android:clickable="true",这样才能响应点击,状态选择器才有效
第三步:写MainActivity
package com.example.mytest002; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { Button button1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1=(Button) findViewById(R.id.button1); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showdialog(); } }); } public void showdialog() { AlertDialog.Builder builder=new Builder(this); AlertDialog dialog=builder.create(); View view=View.inflate(this, R.layout.my_dialog, null); // 去掉上下左右边距, 兼容2.x版本 dialog.setView(view, 0,0,0,0); dialog.show(); } }
标签:
原文地址:http://www.cnblogs.com/xurui1995/p/5723172.html