Android 重写对话框AlertDialog

---xml
<LinearLayout xmlns:android="
http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal"
        android:padding="2dip" >

        <EditText
            android:id="@+id/edittext1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:typeface="monospace"
            android:width="180dip" />
    </LinearLayout>

</LinearLayout>


------.java
LayoutInflater inflater = LayoutInflater.from(EA6Activity.this);
  final View DialogView = inflater

  .inflate(R.layout.dialog, null);
//并成一句
View view=LayoutInflater.from(this).inflate(R.layout.ID, null);   

 


--------AlertDialog创建中输入
builder.setView(DialogView);


------------------重写
WindowManager manager = getWindowManager();
    Display display = manager
      .getDefaultDisplay();
    int width = display
      .getWidth();
    int height = display
      .getHeight();

    LayoutInflater inflater = getLayoutInflater();
    View view = inflater
      .inflate(
        R.layout.dialog,
        null);

    AlertDialog alert = new AlertDialog.Builder(
      DialogActivity.this)
      .create();
    alert.show();

    alert.getWindow()
      .setLayout(
        width / 2,
        height / 4);
    alert.setTitle("测试");
    alert.getWindow()
      .setContentView(
        R.layout.dialog);


-----------------------重写2

    final AlertDialog dlg = new AlertDialog.Builder(
      DialogActivity.this)
      .create();
    dlg.show();
    Window window = dlg
      .getWindow();
    // *** 主要就是在这里实现这种效果的.
    // 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容
    window.setContentView(R.layout.dialog);


---------------------activity
 mButton1.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub

    final AlertDialog dlg = new AlertDialog.Builder(
      DialogActivity.this)
      .create();
    dlg.show();
    Window window = dlg
      .getWindow();
    window.setContentView(R.layout.dialog);

    Button button1 = (Button) window
      .findViewById(R.id.button1);
    button1.setOnClickListener(new OnClickListener() {

     @Override
     public void onClick(
       View v) {
      // TODO Auto-generated method stub

      android.os.Process
        .killProcess(android.os.Process
          .myPid());

     }
    });

   }
  });


-----------------------布局 .xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/icon1"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于 Android 自定义对话框,你可以通过以下步骤来实现: 1. 创建一个新的类继承自 DialogFragment,这将作为你的自定义对话框的实现类。 ```java public class CustomDialogFragment extends DialogFragment { // 在这里实现你的自定义对话框逻辑 } ``` 2. 在你的布局文件中定义自定义对话框的外观。可以使用 LinearLayout、RelativeLayout 或者其他布局容器来构建对话框的外观。 ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!-- 在这里添加对话框的内容,比如文本、按钮等 --> </LinearLayout> ``` 3. 在自定义对话框的实现类中,重写 `onCreateDialog` 方法,将布局文件与对话框关联起来。 ```java @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); LayoutInflater inflater = getActivity().getLayoutInflater(); View dialogView = inflater.inflate(R.layout.custom_dialog_layout, null); builder.setView(dialogView); // 在这里设置对话框的标题、按钮等内容 return builder.create(); } ``` 4. 在需要显示对话框的地方,创建并显示你的自定义对话框实例。 ```java CustomDialogFragment dialog = new CustomDialogFragment(); dialog.show(getSupportFragmentManager(), "custom_dialog"); ``` 这就是一个基本的 Android 自定义对话框的实现过程。你可以进一步扩展自定义对话框的功能,根据你的需求添加文本、按钮、点击事件等等。希望这能帮到你!如果有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值