32、自定义组件RelativeLayout、设置组合控件的状态

2024-05-25 07:18

本文主要是介绍32、自定义组件RelativeLayout、设置组合控件的状态,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


自定义控件步骤:
测量:onMeasure  设置自己显示在屏幕上的宽高
布局:onLayout   设置自己显示在屏幕上的位置(只有在自定义ViewGroup中才用到)
绘制:onDraw     控制显示在屏幕上的样子(自定义viewgroup时不需要这个)


View和ViewGroup的区别
1.他们都需要进行测量操作
2.ViewGroup主要是控制子view如何摆放,所以必须实现onLayout
  View没有子view,所以不需要onLayout方法,但是必须实现onDraw

-----------------------main.java-------------------------------


package com.example.zidingyi;


import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;


public class MainActivity extends ActionBarActivity {


private SettingItemView siv_update;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//自定义组件
siv_update = (SettingItemView) findViewById(R.id.siv_update);
siv_update.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
if(siv_update.isChecked()) {
siv_update.setChecked(false);
} else {

siv_update.setChecked(true);
}
}
});
}


}


-----------------------------SettingItemView.java-------------------------


package com.example.zidingyi;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;


/**
 * 我们自定义的组合控件,它里面有两个TextView ,还有一个CheckBox,还有一个View
 * @author Administrator
 *
 */
public class SettingItemView extends RelativeLayout{

private CheckBox cb_status;
private TextView tv_desc;

/**
* 初始化布局文件
* @param context
*/
private void iniView(Context context) {

//把一个布局文件---》View 并且加载在SettingItemView
View.inflate(context, R.layout.setting_item_view, this);
cb_status = (CheckBox) this.findViewById(R.id.cb_status);
tv_desc = (TextView) this.findViewById(R.id.tv_desc);

}


public SettingItemView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
iniView(context);
}


public SettingItemView(Context context, AttributeSet attrs) {
super(context, attrs);
iniView(context);
}





public SettingItemView(Context context) {
super(context);
iniView(context);
}

/**
* 校验组合控件是否选中
*/

public boolean isChecked(){
return cb_status.isChecked();
}

/**
* 设置组合控件的状态
*/

public void setChecked(boolean checked){
cb_status.setChecked(checked);
if(checked) {
tv_desc.setText("自动更新已经開啟");
} else {
tv_desc.setText("自动更新已经关闭");

}
}

/**
* 设置 组合控件的描述信息
*/

public void setDesc(String text){
tv_desc.setText(text);
}



}


。。。。。。。。。。。。。main.xml。。。。。。。。。。。。。


<?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:orientation="vertical" >


    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="55dip"
        android:background="#8866ff00"
        android:gravity="center"
        android:text="设置中心"
        android:textColor="#000000"
        android:textSize="22sp" />


    <com.example.zidingyi.SettingItemView
        android:id="@+id/siv_update"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </com.example.zidingyi.SettingItemView>


</LinearLayout>


。。。。。。。。。。。。。。setting_item_view.xml。。。。。。。。。。。。


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="68dip" >


        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dip"
            android:layout_marginTop="8dip"
            android:textColor="#000000"
            android:text="设置是否更新"
            android:textSize="20sp" />


        <TextView
            android:id="@+id/tv_desc"
            android:text="自动更新已经关闭"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/tv_title"
            android:layout_marginLeft="10dip"
            android:textColor="#88000000"
            android:textSize="18sp" />


        <CheckBox
            android:clickable="false"
            android:focusable="false"
            android:id="@+id/cb_status"
            android:layout_marginRight="10dip"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        
        <View  
            android:layout_marginLeft="5dip"
            android:layout_marginRight="5dip"
            android:layout_alignParentBottom="true"
            android:background="#000000"
            android:layout_width="fill_parent"
            android:layout_height="0.2dip"/>
    </RelativeLayout>



这篇关于32、自定义组件RelativeLayout、设置组合控件的状态的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1000835

相关文章

Vue和React受控组件的区别小结

《Vue和React受控组件的区别小结》本文主要介绍了Vue和React受控组件的区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录背景React 的实现vue3 的实现写法一:直接修改事件参数写法二:通过ref引用 DOMVu

Vite 打包目录结构自定义配置小结

《Vite打包目录结构自定义配置小结》在Vite工程开发中,默认打包后的dist目录资源常集中在asset目录下,不利于资源管理,本文基于Rollup配置原理,本文就来介绍一下通过Vite配置自定义... 目录一、实现原理二、具体配置步骤1. 基础配置文件2. 配置说明(1)js 资源分离(2)非 JS 资

JWT + 拦截器实现无状态登录系统

《JWT+拦截器实现无状态登录系统》JWT(JSONWebToken)提供了一种无状态的解决方案:用户登录后,服务器返回一个Token,后续请求携带该Token即可完成身份验证,无需服务器存储会话... 目录✅ 引言 一、JWT 是什么? 二、技术选型 三、项目结构 四、核心代码实现4.1 添加依赖(pom

聊聊springboot中如何自定义消息转换器

《聊聊springboot中如何自定义消息转换器》SpringBoot通过HttpMessageConverter处理HTTP数据转换,支持多种媒体类型,接下来通过本文给大家介绍springboot中... 目录核心接口springboot默认提供的转换器如何自定义消息转换器Spring Boot 中的消息

MySQL设置密码复杂度策略的完整步骤(附代码示例)

《MySQL设置密码复杂度策略的完整步骤(附代码示例)》MySQL密码策略还可能包括密码复杂度的检查,如是否要求密码包含大写字母、小写字母、数字和特殊字符等,:本文主要介绍MySQL设置密码复杂度... 目录前言1. 使用 validate_password 插件1.1 启用 validate_passwo

Python自定义异常的全面指南(入门到实践)

《Python自定义异常的全面指南(入门到实践)》想象你正在开发一个银行系统,用户转账时余额不足,如果直接抛出ValueError,调用方很难区分是金额格式错误还是余额不足,这正是Python自定义异... 目录引言:为什么需要自定义异常一、异常基础:先搞懂python的异常体系1.1 异常是什么?1.2

Linux中的自定义协议+序列反序列化用法

《Linux中的自定义协议+序列反序列化用法》文章探讨网络程序在应用层的实现,涉及TCP协议的数据传输机制、结构化数据的序列化与反序列化方法,以及通过JSON和自定义协议构建网络计算器的思路,强调分层... 目录一,再次理解协议二,序列化和反序列化三,实现网络计算器3.1 日志文件3.2Socket.hpp

C语言自定义类型之联合和枚举解读

《C语言自定义类型之联合和枚举解读》联合体共享内存,大小由最大成员决定,遵循对齐规则;枚举类型列举可能值,提升可读性和类型安全性,两者在C语言中用于优化内存和程序效率... 目录一、联合体1.1 联合体类型的声明1.2 联合体的特点1.2.1 特点11.2.2 特点21.2.3 特点31.3 联合体的大小1

python设置环境变量路径实现过程

《python设置环境变量路径实现过程》本文介绍设置Python路径的多种方法:临时设置(Windows用`set`,Linux/macOS用`export`)、永久设置(系统属性或shell配置文件... 目录设置python路径的方法临时设置环境变量(适用于当前会话)永久设置环境变量(Windows系统

深度解析Nginx日志分析与499状态码问题解决

《深度解析Nginx日志分析与499状态码问题解决》在Web服务器运维和性能优化过程中,Nginx日志是排查问题的重要依据,本文将围绕Nginx日志分析、499状态码的成因、排查方法及解决方案展开讨论... 目录前言1. Nginx日志基础1.1 Nginx日志存放位置1.2 Nginx日志格式2. 499