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

相关文章

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定

PyCharm如何设置新建文件默认为LF换行符

《PyCharm如何设置新建文件默认为LF换行符》:本文主要介绍PyCharm如何设置新建文件默认为LF换行符问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录PyCharm设置新建文件默认为LF换行符设置换行符修改换行符总结PyCharm设置新建文件默认为LF

Vue中组件之间传值的六种方式(完整版)

《Vue中组件之间传值的六种方式(完整版)》组件是vue.js最强大的功能之一,而组件实例的作用域是相互独立的,这就意味着不同组件之间的数据无法相互引用,针对不同的使用场景,如何选择行之有效的通信方式... 目录前言方法一、props/$emit1.父组件向子组件传值2.子组件向父组件传值(通过事件形式)方

如何自定义Nginx JSON日志格式配置

《如何自定义NginxJSON日志格式配置》Nginx作为最流行的Web服务器之一,其灵活的日志配置能力允许我们根据需求定制日志格式,本文将详细介绍如何配置Nginx以JSON格式记录访问日志,这种... 目录前言为什么选择jsON格式日志?配置步骤详解1. 安装Nginx服务2. 自定义JSON日志格式各

Linux上设置Ollama服务配置(常用环境变量)

《Linux上设置Ollama服务配置(常用环境变量)》本文主要介绍了Linux上设置Ollama服务配置(常用环境变量),Ollama提供了多种环境变量供配置,如调试模式、模型目录等,下面就来介绍一... 目录在 linux 上设置环境变量配置 OllamPOgxSRJfa手动安装安装特定版本查看日志在

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

基于Spring实现自定义错误信息返回详解

《基于Spring实现自定义错误信息返回详解》这篇文章主要为大家详细介绍了如何基于Spring实现自定义错误信息返回效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录背景目标实现产出背景Spring 提供了 @RestConChina编程trollerAdvice 用来实现 HTT

SpringSecurity 认证、注销、权限控制功能(注销、记住密码、自定义登入页)

《SpringSecurity认证、注销、权限控制功能(注销、记住密码、自定义登入页)》SpringSecurity是一个强大的Java框架,用于保护应用程序的安全性,它提供了一套全面的安全解决方案... 目录简介认识Spring Security“认证”(Authentication)“授权” (Auth

Spring组件初始化扩展点BeanPostProcessor的作用详解

《Spring组件初始化扩展点BeanPostProcessor的作用详解》本文通过实战案例和常见应用场景详细介绍了BeanPostProcessor的使用,并强调了其在Spring扩展中的重要性,感... 目录一、概述二、BeanPostProcessor的作用三、核心方法解析1、postProcessB

Ubuntu中Nginx虚拟主机设置的项目实践

《Ubuntu中Nginx虚拟主机设置的项目实践》通过配置虚拟主机,可以在同一台服务器上运行多个独立的网站,本文主要介绍了Ubuntu中Nginx虚拟主机设置的项目实践,具有一定的参考价值,感兴趣的可... 目录简介安装 Nginx创建虚拟主机1. 创建网站目录2. 创建默认索引文件3. 配置 Nginx4