自定义组合控件

刚刚写了一篇关于Activity继承思想的文章,现在有时间了就给大家写一篇个关于自定义组合控件的博文吧。

对于很多刚刚学习android的人来说,组合控件应该是要学习的,因为组合控件的学习能为你应用的页面布局开发节省不少时间,这样你就能够有更多的时间去完成底层项目啦。废话不多说,下面就奉上一个简单的自定义组合控件的例子。

package defineiew;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.example.qinfuelhyper.myphonesafe.*;
import com.example.qinfuelhyper.myphonesafe.R;

/**这个类是为了setting统一界面节省开发时间而建立的类,
 * 以便于以后所有风格的重用。写多了以后可以将这些东西封装起来以便以后app使用。
 * * Created by FuchaoQin on 2015/11/7.
 */
public class SettingItemView extends RelativeLayout {//我们的这个自定义继承了RelativeLayout,那么就要实现它的构造方法
    //既然是自定义控件,在构造这个控件之前你就得想好这个控件中含有哪些基本控件。下面楼主在这个控件中添加了两个TextView和一个CheckBox.
    private TextView textViewsetting01;
    private TextView textViewsetting02;
    private CheckBox checkBoxsetting;
    private  RelativeLayout relativeLayout;

    public SettingItemView(Context context) {//父类构造函数有三种形式,根据创建对象参数来调用不同的构造方法
        super(context);
        initialViewOfsetting();//复写父类RelativeLayout的函数,让每次使用该View的时候不论是什么形式都调用iniViewOfSetting函数完成初始化
    }

    public SettingItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialViewOfsetting();
    }
    public SettingItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialViewOfsetting();
    }
//这里是自定义控件的重头戏部分了
    public void  initialViewOfsetting(){
        //自定义的View主要部分!初始化一定要调用该函数!!!利用View.inflate完成xml布局的传送
        View view= View.inflate(getContext(),R.layout.layout_setting_view,this);//这里有2个值得注意的地方:参数1,利用getContext()获取Context对象
            textViewsetting01=(TextView)findViewById(R.id.tv_settingRLView01);<span style="white-space:pre">	</span> //参数3,将我们自定义的组合View塞给this,这里this是RelativeLayout
            textViewsetting02=(TextView)findViewById(R.id.tv_settingRLView02);<span style="white-space:pre">	</span> //<span style="white-space:pre">			</span>
            checkBoxsetting=(CheckBox)findViewById(R.id.cb_settingRLCheckBox);
            relativeLayout=(RelativeLayout)findViewById(R.id.settingRLT);
    }
    public void setRLTitle(String string){<span style="white-space:pre">	</span>//大家在使用控件的时候都会调用一些函数来完成自己的需求,我们也可以为自定义的View设置相应的函数
        textViewsetting01.setText(string);//接口1,设置标题
    }
    public void setRLContext(String string){
        textViewsetting02.setText(string);//接口2,设置内容
    }
    public boolean isChecked(){
        return checkBoxsetting.isChecked();//接口三,检查勾选框状态
    }
    public void setSetingChecked(boolean checked){
        checkBoxsetting.setChecked(checked);//设置勾选框状态
    }
    public void setRLBackground( int color){//设置背景relative背景
        relativeLayout.setBackgroundColor(color);
    }
    public void Summerize(){
        /*
        * 这个SettingItemView就是一个自定义的组合控件,提高了代码的重用性,提高开发效率。
        * 自定义一个View 继承ViewGroup 例如RelativeLayout或者LinearLayout,复写父类函数并且自定义一个初始化函数,放入复写函数中
        * 创建好一个xml文件。  利用View view=View.inflate(getContext(),xml名称,this)完成自定义组合控件与父空间的结合
        * 暴露一些接口。
        * 可以自定义属性。
        * */
    }

}
接下来给大家贴上XML的代码。

<?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="90dp"
        android:background="#b4d9ff00"
        android:id="@+id/settingRLT"
        >
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="35sp"
            android:id="@+id/tv_settingRLView01"
            />
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="19sp"
            android:layout_below="@+id/tv_settingRLView01"
            android:layout_alignParentBottom="true"
            android:gravity="bottom"
            android:id="@+id/tv_settingRLView02"
            />
        <CheckBox
            android:checked="false"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:clickable="false"//这里的注释方式不规范,但是我想说的是一般CheckBox中的clickable和focusable、focusableInTouchMode都在一起使用的
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:id="@+id/cb_settingRLCheckBox"
            >
        </CheckBox>
    </RelativeLayout>

自定义控件差不多就说到这里,在activity相应的xml文件中要引用该自定义的View记得把包名写在控件的前面,不然是识别不出来的!

关于页面的布局,还有一点小小的东西想分享给大家。

比如说,我们组合控件中的TextView在不同的Activity页面中需要展示不同的样式,这个时候我们不想又去写一个自定义的组合控件,这个时候我们怎么办呢?

其实,对于已有的控件,我们可以在res/values中为他们设置不同的样式(博主用的是Android studio,如果用eclipse的小伙伴请自己去找values这个文件夹了啊)

例如,我们可以在style中为TextView设置样式:(大家在XML中利用style="@style/name"即可插入该风格!省略了一堆的代码,同时风格是以name来区分的)

 <style name="functionTitle">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">78dp</item>
        <item name="android:background">@color/colorTitleBackground</item>
        <item name="android:textSize">40sp</item>
        <item name="android:gravity">center</item>
        <item name="android:textColor">@color/colorTitleText</item>
    </style>
当然啦,依葫芦画瓢,在color里面我们也可以设置自己喜欢的颜色,调用方法与style类似。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值