首页 > 其他分享 >自定义样式与主题

自定义样式与主题

时间:2022-12-01 13:04:27浏览次数:36  
标签:控件 des 自定义 样式 主题 update sv setting


#样式与主题(重点)

##01_样式
样式:主要作用于控件上的,修饰控件的一些属性;

自定义样式:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="button_color_stype">
<item name="android:background" >#ff0000</item>
<item name="android:textColor">#00ff00</item>
</style>

<style name="textview_color_stype">
<item name="android:background" >#ff0000</item>
<item name="android:textColor">#0000ff</item>
</style>

<style name="textview_color_stype_largesize" parent="textview_color_stype">
<item name="android:textSize" >20sp</item>
<item name="android:background" >#0000ff</item>
</style>

</resources>

##02_主题

主题:界面或者整个应用程序的风格;
theme
定义主题的方法与定义样式完全一样;

代码:
<?xml version="1.0" encoding="utf-8"?>

</resources>


<activity
android:name="com.itheima.style.MainActivity"
android:label="@string/app_name"
android:theme="@style/my_theme_activity_background"
>

自定义组合控件# ***

public class SettingView extends RelativeLayout {

//在代码中使用的时候调用
public SettingView(Context context) {
super(context);
//实现不管采用哪种方式使用自定义组合控件,都能调用init()方法
init();

}
//在布局文件使用的时候调用 ,多了样式文件
public SettingView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);

init();

}
//在布局文件使用的时候调用
public SettingView(Context context, AttributeSet attrs) {
super(context, attrs);

init();

}
/**
* 添加布局文件
*/
private void init(){
//添加布局文件
// TextView textView = new TextView(getContext());
// textView.setText("这是一个自定义组合控件的textview");
//第一种方式
//将布局文件那转化成view对象
// View view = View.inflate(getContext(), R.layout.settingview, null);//爹有了,直接找孩子,亲生的
// //添加
// this.addView(view);//给相对布局添加一个textview
// this.addView(child, params);//layoutParams : 用代码给控件设置属性,就表示要设置控件在自定义空间中的属性
//第二种方式
//root:给view对象找一个父控件
View.inflate(getContext(), R.layout.settingview, this);//孩子有了,直接找爹,喜当爹,不使用,不设置属性,父控件会使用原控件的属性
}

}

#自定义控件的点击事件# **

1.将自定义组合控件移植到手机卫士
2.在自定义组合控件中添加改变控件状态的方法,来方便我们改变自定义控件中控件的值

/**
* 设置标题
* @param title
*/
public void setTitle(String title){
tv_setting_title.setText(title);
}
/**
* 设置描述信息
* @param des
*/
public void setDes(String des){
tv_setting_des.setText(des);
}
/**
* 设置checkbox的状态
* @param isChecked
*/
public void setChecked(boolean isChecked){
cb_setting_isupdate.setChecked(isChecked);
}
/**
* 获取checkbox的状态
* @return
*/
public boolean isChecked(){
//isChecked();获取checkbox的状态
return cb_setting_isupdate.isChecked();
}
3.在activity中使用相应的方法
//初始化自定义组合控件中的控件的值
sv_setting_update.setTitle("提示更新");
sv_setting_update.setDes("打开提示更新");
sv_setting_update.setChecked(true);

sv_setting_update.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
if (sv_setting_update.isChecked()) {
//关闭提示更新
sv_setting_update.setDes("关闭提示更新");
sv_setting_update.setChecked(false);
}else{
//打开提示更新
sv_setting_update.setDes("打开提示更新");
sv_setting_update.setChecked(true);
}
}
});

自定义属性# **

查看系统属性文件:sdk\platforms\android-16\data\res\values\attrs.xml

1.res -> values -> attrs.xml(固定写法)
<resources>
<declare-styleable name="cn.itcast.mobilesafexian02.ui.SettingView">
<attr name="title" format="string" /><!-- format : 格式 类型-->
<attr name="des_on" format="string" />
<attr name="des_off" format="string" />
</declare-styleable>
</resources>
2.到布局文件中使用自定义属性

命名空间:xmlns:itcast="http://schemas.android.com/apk/res/cn.itcast.mobilesafexian02"

<cn.itcast.mobilesafexian02.ui.SettingView
android:id="@+id/sv_setting_update"
android:layout_width="match_parent"
android:layout_height="wrap_content"
itcast:title="提示更新"
itcast:des_on="打开提示更新"
itcast:des_off="关闭提示更新"
></cn.itcast.mobilesafexian02.ui.SettingView>

3.在代码中使用,是自定义属性有意义,两个参数的构造函数中
//在布局文件使用的时候调用
public SettingView(Context context, AttributeSet attrs) {
super(context, attrs);

init();

//AttributeSet保存有控件的所有属性,通过AttributeSet获取控件的所有属性了
// int count = attrs.getAttributeCount();//获取控件属性的个数
// System.out.println("属性个数:"+count);
// for (int i = 0; i < count; i++) {
// //获取相应的属性的值
// System.out.println(attrs.getAttributeValue(i));
// }
//通过命名空间和属性的名称获取属性的值
//namespace : 命名空间
//name : 属性的名称
String title = attrs.getAttributeValue("http://schemas.android.com/apk/res/cn.itcast.mobilesafexian02", "title");
des_on = attrs.getAttributeValue("http://schemas.android.com/apk/res/cn.itcast.mobilesafexian02", "des_on");
des_off = attrs.getAttributeValue("http://schemas.android.com/apk/res/cn.itcast.mobilesafexian02", "des_off");
//使用获取的属性的值
//设置标题控件的值
tv_setting_title.setText(title);
//设置描述信息的值
if (isChecked()) {
tv_setting_des.setText(des_on);
}else{
tv_setting_des.setText(des_off);
}

}
在setChecked方法中增加
/**
* 设置checkbox的状态
* @param isChecked
*/
public void setChecked(boolean isChecked){
cb_setting_isupdate.setChecked(isChecked);
//相当于将sv_setting_update.setDes("打开提示更新");封装到了这个方法中
if (isChecked()) {
tv_setting_des.setText(des_on);
}else{
tv_setting_des.setText(des_off);
}
}
4.在activity中去除相应的方法

sv_setting_update = (SettingView) findViewById(R.id.sv_setting_update);

//初始化自定义组合控件中的控件的值

// sv_setting_update.setTitle(“提示更新”);
//根据保存的状态设置初始化的状态
//defValue : 没有update的时候使用的值
if (sp.getBoolean(“update”, true)) {
// sv_setting_update.setDes(“打开提示更新”);
sv_setting_update.setChecked(true);
}else{
// sv_setting_update.setDes(“关闭提示更新”);
sv_setting_update.setChecked(false);
}

sv_setting_update.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Editor edit = sp.edit();
if (sv_setting_update.isChecked()) {
//关闭提示更新

// sv_setting_update.setDes(“关闭提示更新”);
sv_setting_update.setChecked(false);
edit.putBoolean(“update”, false);
// edit.apply();//可以保存到文件中,但是仅限于9版本之上,9版本之下是保存到内存中的
}else{
//打开提示更新
// sv_setting_update.setDes(“打开提示更新”);
sv_setting_update.setChecked(true);
edit.putBoolean(“update”, true);
}
edit.commit();
}
});


标签:控件,des,自定义,样式,主题,update,sv,setting
From: https://blog.51cto.com/u_15898516/5901798

相关文章