Android自定义控件——MIUIDialog
2018-12-03 本文已影响0人
WangSins
我们在Android开发过程中,很多时候时候官方所提供控件不能满足我们现有的需求,这时候我们就需要自定义控件。
自定义控件三种实现方式:
- 继承已有控件
- 组合已有控件
- 自定义控件/View
下面就通过一个小案例对自定义控件中的继承已有控件进行学习和总结:
首先我们得明白继承现有控件的意思,继承已有控件就是:继承已有的控件,创建新控件,保留继承的父控件的特性,并且还可以引入新特性。
自定义已有控件
代码实现仿MUIDailog
- 首创建自定义Dailog的布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/shap_custom_dailog"
android:orientation="vertical">
<TextView
android:id="@+id/dl_titel_miui"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="提示"
android:textColor="#000"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/dl_content_miui"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="20sp"
android:layout_marginBottom="20dp"
android:text="提示内容"
android:textColor="#000"
android:textSize="20sp" />
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#666" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="horizontal">
<TextView
android:id="@+id/btn_cancel_miui"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="取消"
android:textColor="#444"
android:textSize="20sp" />
<View
android:layout_width="0.5dp"
android:layout_height="match_parent"
android:background="#666" />
<TextView
android:id="@+id/btn_confirm_miui"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="确定"
android:textColor="#0fa8ce"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
- 创建自己的Dlilog继承自Dailog
import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
public class MIUIDialog extends Dialog {
private TextView mTitle, mContent, mCancel, mConfirm;
private String miuidlTitle, miuidlContent, miuidlCancel, miuidlConfirm;
private MIUIonCancelListener cancelListener;
private MIUIonConfirmListener confirmListener;
public MIUIDialog(@NonNull Context context) {
super(context, R.style.MIUIDialog);
}
public MIUIDialog(@NonNull Context context, int themeResId) {
super(context, themeResId);
}
public void setMiuidlTitle(String miuidlTitle) {
this.miuidlTitle = miuidlTitle;
}
public void setMiuidlContent(String miuidlContent) {
this.miuidlContent = miuidlContent;
}
public void setDlCancel(String dlCancel, MIUIonCancelListener listener) {
this.miuidlCancel = dlCancel;
this.cancelListener = listener;
}
public void setDlConfirm(String dlConfirm, MIUIonConfirmListener listener) {
this.miuidlConfirm = dlConfirm;
this.confirmListener = listener;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();//初始化界面控件
initScreenPosition();//设置窗口位置
initData();//初始化数据
initEvent();//初始化界面监听器,向外界提供监听事件
}
private void initEvent() {
mCancel.setOnClickListener(v -> {
if (cancelListener != null) {
cancelListener.Cancel(this);
dismiss();
}
});
mConfirm.setOnClickListener(v -> {
if (confirmListener != null) {
confirmListener.Confirm(this);
dismiss();
}
});
}
private void initView() {
setContentView(R.layout.dialog_miui);
setCanceledOnTouchOutside(false);//设置不能通过点击空白部分取消Dialog
mTitle = findViewById(R.id.dl_titel_miui);
mContent = findViewById(R.id.dl_content_miui);
mCancel = findViewById(R.id.btn_cancel_miui);
mConfirm = findViewById(R.id.btn_confirm_miui);
}
private void initScreenPosition() {
Window dialogWindows = getWindow();
dialogWindows.setGravity(Gravity.BOTTOM);
WindowManager.LayoutParams lp = dialogWindows.getAttributes();
Display d = getWindow().getWindowManager().getDefaultDisplay();
Point size = new Point();
d.getSize(size);
lp.width = (int) (size.x * 0.95);
lp.y = 15;
dialogWindows.setAttributes(lp);
}
private void initData() {
if (!TextUtils.isEmpty(miuidlTitle)) {
mTitle.setText(miuidlTitle);
}
if (!TextUtils.isEmpty(miuidlContent)) {
mContent.setText(miuidlContent);
}
if (!TextUtils.isEmpty(miuidlCancel)) {
mCancel.setText(miuidlCancel);
}
if (!TextUtils.isEmpty(miuidlConfirm)) {
mConfirm.setText(miuidlConfirm);
}
}
public interface MIUIonCancelListener {
void Cancel(MIUIDialog dialog);
}
public interface MIUIonConfirmListener {
void Confirm(MIUIDialog dialog);
}
}
3.自定义主题
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="MIUIDialog" parent="android:style/Theme.Dialog">
<!--背景颜色及和透明程度-->
<item name="android:windowBackground">@android:color/transparent</item>
<!--是否去除标题 -->
<item name="android:windowNoTitle">true</item>
<!--是否去除边框-->
<item name="android:windowFrame">@null</item>
</style>
</resources>
- 最后在需要的地方创建即可