1.Activity.this与getApplicationContext()的区别:
对话框是Activity的一部分。
对话框是挂载在Activity上面的 。
如果Activity不存在,对话框就不能被创建。
Activity 实际上是应用程序context上下文的一个子集。
子类有的东西父类不一定有
父类有的东西子类一定有
getApplicationContext();生命周期长,只要应用还存活它就存在;
this 生命周期短,只要Activity不存在了,系统就会回收;
其中:getBaseContext(),getApplication(),getApplicationContext();
都不能放在AlertDialog做上下文;
getApplicationContext() 使用场景是比如频繁需要操作的数据库
推荐用法:Activity.this
2.定义自定义控件的属性:
1、声明一个View对象 继承相对布局,或者线性布局或者其他的ViewGroup。
2、在自定义的View对象里面重写它的构造方法。在构造方法里面就把布局都初始化完毕。
3、根据业务需求 添加一些api方法,扩展自定义的组合控件;
4、希望在布局文件里面 可以自定义一些属性。
5、声明自定义属性的命名空间。
xmlns:itheima="http://schemas.android.com/apk/res/com.itheima.mobilesafe"
6、在res目录下的values目录下创建attrs.xml的文件 声明你写的属性。
<declare-styleable name="SettingItemView">
<attr name="title" format="string" />
<attr name="desc_on" format="string" />
<attr name="desc_off" format="string" />
</declare-styleable>
7、在布局文件中写哪些你自定义的属性。
8、使用这些定义的属性。自定义View对象的构造方法里面 有一个带两个参数的构造方法
布局文件里面定义的属性都放在 AttributeSet attrs
获取那些定义的属性。
SettingItemView.java
package com.konka.tokgo.mobilesafe.view;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.konka.tokgo.mobilesafe.R;
/**
* Created by Tokgo on 2015-12-11.
*/
public class SettingItemView extends RelativeLayout {
private static final String NAMESPACE = "http://schemas.android.com/apk/res/com.konka.tokgo.mobilesafe";
TextView mTvTitle;
TextView mTvDesc;
CheckBox mCbStatus;
String mTitle;
String mDescOn;
String mDescOff;
public SettingItemView(Context context) {
super(context);
initView();
}
public SettingItemView(Context context, AttributeSet attrs) {
super(context, attrs);
//根据属性名称获取属性的值
mTitle = attrs.getAttributeValue(NAMESPACE, "titles");
mDescOn = attrs.getAttributeValue(NAMESPACE, "desc_on");
mDescOff = attrs.getAttributeValue(NAMESPACE, "desc_off");
initView();
}
public SettingItemView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
/**
* 初始化布局
*/
private void initView() {
//将自定义的布局文件设置给SettingItemView
View.inflate(getContext(), R.layout.view_setting_item, this);
mTvTitle = (TextView) findViewById(R.id.tv_title);
mTvDesc = (TextView) findViewById(R.id.tv_desc);
mCbStatus = (CheckBox) findViewById(R.id.cb_status);
setTitle(mTitle);
}
/**
* 设置标题
* */
public void setTitle(String title) {
mTvTitle.setText(title);
}
public void setDesc(String desc) {
mTvDesc.setText(desc);
}
/**
* 返回勾选状态
* */
public boolean isChecked() {
return mCbStatus.isChecked();
}
/**
* 设置勾选状态
* */
public void setChecked(boolean check) {
mCbStatus.setChecked(check);
if(check) {
setDesc(mDescOn);
} else {
setDesc(mDescOff);
}
Log.d("TAG",mDescOff+mDescOn);
}
}
<span style="font-size:18px;">view_setting_item.xml</span><span style="font-size:18px;"></span><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_below="@+id/textView"
android:padding="5dp">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
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="#aa000000"
android:textSize="18sp" />
<CheckBox
android:id="@+id/cb_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_centerVertical="true" />
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_alignParentBottom="true"
android:background="#aa000000" />
</RelativeLayout>调用:
<com.konka.tokgo.mobilesafe.view.SettingItemView android:id="@+id/siv_update" tokgo:titles="自动更新设置" tokgo:desc_on="自动更新已开启" tokgo:desc_off="自动更新已关闭" android:layout_width="match_parent" android:layout_height="wrap_content" />
本文深入探讨了Android自定义控件的属性定义与布局实现,同时对比了Activity的几种上下文使用方式及其应用场景。通过实例解析,帮助开发者掌握自定义控件的构造与属性设置,以及Activity上下文的生命周期与正确使用方法。
1020

被折叠的 条评论
为什么被折叠?



