23. android dialog——自定义对话框之三(带style的)

时间来不及,就贴代码了,另外顺便贴一下Activity模拟成一个Dialog,具体可以参考下面的网址

part1  http://blog.stylingandroid.com/archives/5

part2  http://blog.stylingandroid.com/archives/7

part3  http://blog.stylingandroid.com/archives/category/dialog-box

 

接下来是一个自定义对话框

第一步:

res/values/styles.xml

<?xml version="1.0" encoding="utf-8"?> <resources> <style name="base"> <item name="android:padding">10dp</item> </style> <style name="title" parent="@style/base"> <item name="android:textColor">#FFFFFF</item> <item name="android:textSize">16sp</item> <item name="android:textStyle">bold</item> <item name="android:background">#333333</item> </style> <style name="body" parent="@style/base"> <item name="android:textColor">#FFFFFF</item> <item name="android:background">#666666</item> </style> <style name="dialog" parent="@android:style/Theme.Dialog"> <item name="android:windowIsFloating">true</item> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@null</item> </style> </resources>

 

 第二步:

res/layout/customer_dialog.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="match_parent" android:id="@+id/title" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_height="wrap_content" style="@+style/title" android:text="自定义对话框" /> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_alignLeft="@id/title" android:layout_below="@id/title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingRight="10dip" android:paddingLeft="10dip" android:id="@+id/body" style="@+style/body" mce_style="@+style/body"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18dip" android:text="用户名:" android:paddingRight="10dip" /> <EditText android:id="@+id/username" android:text="输入用户名" android:layout_width="fill_parent" android:layout_height="wrap_content" android:lines="1" /> </LinearLayout> </RelativeLayout>

 

第三步:

res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="显示自定义对话框" /> </LinearLayout>

 

第四步:

src/com/dialog/ui/CustomerDialog.java

package com.dialog.ui; import android.app.AlertDialog; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import com.dialog.activity.R; public class CustomerDialog extends AlertDialog { public CustomerDialog(Context context) { super(context); } public CustomerDialog(Context context, int theme) { super(context, theme); } @Override protected void onCreate(Bundle savedInstanceState) { //关于LayoutInflate详解 //http://blog.csdn.net/jamesliulyc/archive/2011/04/14/6324432.aspx LayoutInflater inflater = LayoutInflater.from(getContext()); View customerLayout = inflater.inflate(R.layout.customer_dialog, null); setView(customerLayout,0,0,0,0); super.onCreate(savedInstanceState); } }

 

第五步:

src/com/dialog/activity/CustomerDialogActivity.java

package com.dialog.activity; import android.app.Activity; import android.app.Dialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; import com.dialog.ui.CustomerDialog; public class CustomerDialogActivity extends Activity { private final int CUSTOMER_DIALOG = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button = (Button) findViewById(R.id.button); View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View view) { showDialog(CUSTOMER_DIALOG); } }; button.setOnClickListener(listener); } @Override protected Dialog onCreateDialog(int id) { CustomerDialog dialog = null; switch(id) { case CUSTOMER_DIALOG: dialog = new CustomerDialog(CustomerDialogActivity.this,R.style.dialog); DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(CustomerDialogActivity.this, "这是一个自定义对话框", Toast.LENGTH_LONG).show(); } }; dialog.setButton("确定", listener); dialog.setButton2("取消", listener); break; } return dialog; } }

 

效果图:

 

23. android dialog——自定义对话框之三(带style的)_第1张图片

 

 

你可能感兴趣的:(android,layout,Class,dialog,button,encoding)