Android自学 -- 自定义对话框

Android自学 – 自定义对话框

1

今天在设计一个自定义的类似于ios的对话框时,发现了一个问题。
没有显示出我想要的效果。
我想实现的效果
在这里插入图片描述
代码如下:
设置一个矩形框的样式mdialog_bg.xml,四周设置角度16px;

<?xml version="1.0" encoding="utf-8"?>
<!--圆角矩形背景-->
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
    <!--设置背景颜色-->
    <solid android:color="@color/white" />
    <!--设置圆角的角度-->
    <corners android:radius="16dp" />
</shape>

设置布局layout

<?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"
    android:orientation="vertical"
    android:background="@drawable/mdialog_bg"
    >
    <!--对话框标题区域-->
    <RelativeLayout
        android:layout_width="@dimen/layout_width"
        android:layout_height="@dimen/layout_height"
        android:id="@+id/layout_title"
        android:gravity="center">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/title"
            android:text="@string/text_title"
            android:textColor="@color/black"
            android:gravity="center"
            />

    </RelativeLayout>
    <!--对话框横线-->
    <View
        android:layout_width="@dimen/layout_width"
        android:layout_height="1dp"
        android:background="@color/grey"
        android:layout_below="@+id/layout_title"
        android:layout_marginBottom="10dp"
        />
<!--对话框按钮区域-->
    <RelativeLayout
        android:id="@+id/bottomoll"
        android:layout_width="@dimen/layout_width"
        android:layout_height="wrap_content"
        android:layout_below="@+id/layout_title"
        >
        <LinearLayout
            android:layout_width="@dimen/layout_width"
            android:layout_height="wrap_content">
            <!--取消按钮-->
            <Button
                android:id="@+id/btn_cancel"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/text_cancel"
                android:textColor="@color/blue"
                android:background="@null"
                android:textSize="18sp"
                android:paddingTop="10dp"
                android:paddingBottom="10dp"
                />

            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="@color/grey"
                />
            <!--退出按钮-->
            <Button
                android:id="@+id/btn_exit"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/text_exit"
                android:textColor="@color/blue"
                android:background="@null"
                android:textSize="18sp"

                />
        </LinearLayout>
    </RelativeLayout>


</RelativeLayout>

编写自定义类,继承Dialog

package view;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.mingrisoft.iosdialog.R;


public class Mdialog extends Dialog {
    TextView tv;
    Button btn_cancel;
    Button btn_exit;

    public Mdialog(Context context) {
        super(context);
        
//        View view = LayoutInflater.from(getContext()).inflate(R.layout.mdialoglayout, null);
//        tv = findViewById(R.id.title);
//        btn_cancel = view.findViewById(R.id.btn_cancel);
//        btn_exit = view.findViewById(R.id.btn_exit);
//        setContentView(view);
        setContentView(R.layout.mdialoglayout);
        tv = findViewById(R.id.title);
        btn_cancel = findViewById(R.id.btn_cancel);
        btn_exit = findViewById(R.id.btn_exit);

    }

    //设置显示的标题文字
    public void setTv(String content) {
        tv.setText(content);
    }

    //取消按钮监听
    public void setOnCancelListener(View.OnClickListener listener) {
        btn_cancel.setOnClickListener(listener);
    }

    //退出按钮监听
    public void setOnExitListener(View.OnClickListener listener) {
        btn_exit.setOnClickListener(listener);
    }
}

最后MainActivity里 调用显示。
可是效果还是这样的
在这里插入图片描述
不应该啊姐妹。

2

后来研究了一下,好像是因为其实已经实现了,但是原本的Dialog本来就有一个矩形的背景,你的圆角矩形已经显示了,不过没有边框,他盖在原本的矩形之上,你看不出来,怎么办呢,把背景隐藏掉。
设计一个对话框的样式,继承自Dialog,在style.xml

    <!--对话框样式-->
    <style name="mdialog" parent="android:style/Theme.Dialog">
        <!-- 背景透明 -->
        <item name="android:windowBackground">@android:color/transparent</item>
    </style>

在自定义Dialog中super父类构造的时候把这个东西传过去。

package view;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.mingrisoft.iosdialog.R;


public class Mdialog extends Dialog {
    TextView tv;
    Button btn_cancel;
    Button btn_exit;

    public Mdialog(Context context) {
        super(context, R.style.mdialog);

//        View view = LayoutInflater.from(getContext()).inflate(R.layout.mdialoglayout, null);
//        tv = findViewById(R.id.title);
//        btn_cancel = view.findViewById(R.id.btn_cancel);
//        btn_exit = view.findViewById(R.id.btn_exit);
//        setContentView(view);

        setContentView(R.layout.mdialoglayout);
        tv = findViewById(R.id.title);
        btn_cancel = findViewById(R.id.btn_cancel);
        btn_exit = findViewById(R.id.btn_exit);

    }

    //设置显示的标题文字
    public void setTv(String content) {
        tv.setText(content);
    }

    //取消按钮监听
    public void setOnCancelListener(View.OnClickListener listener) {
        btn_cancel.setOnClickListener(listener);
    }

    //退出按钮监听
    public void setOnExitListener(View.OnClickListener listener) {
        btn_exit.setOnClickListener(listener);
    }
}

再试一下,成了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值