设置自定义控件view(自定义相对布局和对话框)

自定义相对布局

1.先设置好要自定义成View的layout布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:padding="5dp">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/color_Text"
        android:textSize="22sp"
        />

    <TextView
        android:id="@+id/tv_desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_title"
        android:layout_marginTop="3dp"
        android:textColor="#a000"
        android:textSize="18sp"
        />

    <CheckBox
        android:id="@+id/cb_status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false" />

    <View
        android:layout_width="match_parent"
        android:layout_height="0.2dp"
        android:layout_alignParentBottom="true"
        android:background="#a000" />
</RelativeLayout>

2.新建attrs.xml,加入属性名和值类型,创建属性是为了暴露出一些接口给我们方便写值

<resources>

    <declare-styleable name="SettingItemView">
        <attr name="set_title" format="string" />
        <attr name="desc_on" format="string" />
        <attr name="desc_off" format="string" />
    </declare-styleable>

</resources>

3.创建class并继承父类(这里父类可以是别的viewGroup)

public class SettingItemView extends RelativeLayout
 
 
4. 继承父类的3个构造方法,自定义初始化方法覆盖父类构造方法,和从布局文件中获取定义的属性值
//命名空间,在调用此自定义View的布局文件中需要设置,必须为项目包名
private static final String NAMESPACE = "http://schemas.android.com/apk/res/com.example.wanghao.didisafe";
// 用代码new对象时,走此方法
public SettingItemView(Context context) {
    super(context);
    initView();//初始化布局
}

// 有属性时走此方法
public SettingItemView(Context context, AttributeSet attrs) {
    super(context, attrs);

    mtitle = attrs.getAttributeValue(NAMESPACE, "set_title");
    mdesc_on = attrs.getAttributeValue(NAMESPACE, "desc_on");
    mdesc_off = attrs.getAttributeValue(NAMESPACE, "desc_off");

    Log.d("TAG",mtitle+"__"+mdesc_on+"_______"+mdesc_off);
    initView();
}

// style样式的话会走此方法
public SettingItemView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initView();
}

private void initView() {
    //将定义好的子项布局文件加载给自定义view,并传给当前的class实例
    View.inflate(getContext(), R.layout.setting_item_view, this);
    //这里获取子项实例不需要view.是应为传入的class的实例。
    tv_title = (TextView) findViewById(R.id.tv_title);
    tv_desc = (TextView) findViewById(R.id.tv_desc);
    cb_status = (CheckBox) findViewById(R.id.cb_status);

    setTitle(mtitle);//这里要特别注意,必须要我们手动在这里或者外部类条用方法,来把属性取的值填入自定义控件
}
5.自定义方法,public的方法在拿到自定义view的实例后都是可以在外部类里调用的
private void setTitle(String mtitle) {
    tv_title.setText(mtitle);
}

private void setDesc(String mdesc){
    tv_desc.setText(mdesc);
}


public void setChecked(Boolean checked) {
    cb_status.setChecked(checked);
    if (checked){
        setDesc(mdesc_on);
    }else {
        setDesc(mdesc_off);
    }
}

public boolean isChecked() {
    checked = cb_status.isChecked();
    return checked;
}
6.在别的布局文件中调用自定义view
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
这行是要我们自己添加的,wanghao是自己随意定义的,后面的则必须上项目包名
xmlns:wanghao="http://schemas.android.com/apk/res/com.example.wanghao.didisafe"

    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        style="@style/TV_Style_Title"
        android:text="设置中心"/>
    <com.example.wanghao.didisafe.view.SettingItemView
        android:id="@+id/siv_update"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        wanghao:set_title="自动更新设置"
        wanghao:desc_off= "自动更新已关闭"
        wanghao:desc_on="自动更新已开启"
        ></com.example.wanghao.didisafe.view.SettingItemView>
7.在外部类里调用自定义的方法
msiv_update = (SettingItemView) findViewById(R.id.siv_update);
mauto_update = mconfig.getBoolean("auto_update", true);

//判断用户是否已经勾选
if (mauto_update) {
    msiv_update.setChecked(true);
} else {
    msiv_update.setChecked(false);

}
自定义对话框
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final AlertDialog dialog = builder.create();

View view = View.inflate(this, R.layout.input_password_dailog, null);
// dialog.setView(view);// 将自定义的布局文件设置给dialog
dialog.setView(view, 0, 0, 0, 0);// 设置边距为0,保证在2.x的版本上运行没问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android自定义对话框可以通过以下步骤实现: 1. 创建一个布局文件,定义对话框的UI界面。 2. 在Activity中实例化该布局文件。可以使用LayoutInflater类将布局文件解析为View对象。 3. 设置对话框的样式,比如设置对话框的标题,设置对话框的大小等。 4. 设置对话框的显示内容。可以通过findViewById方法获取对话框中的控件,并设置其显示内容。 5. 设置对话框的按钮事件。通过findViewById方法获取对话框中的按钮,并为按钮设置监听器,实现对按钮的点击事件进行处理。 6. 显示对话框。通过调用show方法显示对话框。 示例代码如下: ```java public class MyDialog extends Dialog { private TextView mTitle; private EditText mContent; private Button mConfirmBtn; private Button mCancelBtn; public MyDialog(Context context, int themeResId) { super(context, themeResId); setContentView(R.layout.dialog_layout); mTitle = findViewById(R.id.dialog_title); mContent = findViewById(R.id.dialog_content); mConfirmBtn = findViewById(R.id.dialog_confirm); mCancelBtn = findViewById(R.id.dialog_cancel); mConfirmBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 处理确认按钮的点击事件 dismiss(); } }); mCancelBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 处理取消按钮的点击事件 dismiss(); } }); } public void setTitle(String title) { mTitle.setText(title); } public void setContent(String content) { mContent.setText(content); } } ``` 在Activity中使用该对话框: ```java MyDialog dialog = new MyDialog(this, R.style.dialog); dialog.setTitle("提示"); dialog.setContent("确定要删除吗?"); dialog.show(); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值