自定义 Dialog

本文介绍如何在Android中自定义Dialog,包括创建自定义Dialog类、界面定制方法及使用示例。自定义Dialog能够帮助开发者更好地匹配应用风格,提升用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自定义 Dialog

[功能]
android 提供给我们的只有2种Dialog 即 AlertDialog & ProgressDialog 但是 Dialog 有其自身的特点:1. 不是 Activity 2. 开销比 Activity 小得多
鉴于以上的优点 我们就有定制自己Dialog 的需求

[原理]
1. android 系统提供了一个class: Dialog. 而且你可以把自己的工作放在"protected void onCreate(Bundle savedInstanceState)" 在里面先调用系统的"super.onCreate(savedInstanceState)" 其就会保证调用这个method.
2. 至于 Dialog 界面的定制 可以写一个xml 文件 然后 在 "void onCreate(Bundle)" 通过 "setContentView()" 来使之生效
3. Dialog 使用问题: 1. 弹出:show() 2. 取消:dismiss()


[代码]
1. 创建一个 Dialog .
public class CustomDialog extends Dialog {

public CustomDialog(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

setContentView(R.layout.custom_dialog);
setTitle("Custom Dialog");

TextView text = (TextView)findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView)findViewById(R.id.image);
image.setImageResource(R.drawable.sepurple);

Button buttonYes = (Button) findViewById(R.id.button_yes);
buttonYes.setHeight(5);
buttonYes.setOnClickListener(new Button.OnClickListener(){

public void onClick(View v) {
// TODO Auto-generated method stub
dismiss();

}
});
Button buttonNo = (Button) findViewById(R.id.button_no);
buttonNo.setSingleLine(true);
buttonNo.setOnClickListener(new Button.OnClickListener(){

public void onClick(View v) {
// TODO Auto-generated method stub
dismiss();

}
});
}

//called when this dialog is dismissed
protected void onStop() {
Log.d("TAG","+++++++++++++++++++++++++++");
}


}



2. Dialog 的使用:
public class CustomDialogUsage extends Activity {
CustomDialog cd;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

cd = new CustomDialog(this);


Button buttonYes = (Button) findViewById(R.id.main_button);
buttonYes.setOnClickListener(new OnClickListener(){

public void onClick(View v) {
// TODO Auto-generated method stub
cd.show();

}
});

}

}


3. Dialog 的界面定制.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
>
<ImageView android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5px"
>
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5px"
>
<Button android:id="@+id/button_yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Yes "
android:gravity="center"
/>
<Button android:id="@+id/button_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" No "
android:gravity="center"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>



done!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值