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);
}
}
再试一下,成了。