温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

简单的自定义小控件,复用标题栏

发布时间:2020-07-18 20:09:16 来源:网络 阅读:439 作者:老司基 栏目:移动开发

    一般情况下actionBar我都会隐藏掉,自己定义一个标题栏来用,一为了方便,二是如果用系统的actionbar来实现同样的效果,很复杂。

简单的写一个例子

简单的自定义小控件,复用标题栏

相当简单的一个东西,一般情况向在布局的最上端include一下就好了,很久以前我也这么做,但是如果俩边的图片想换一下呢?中间的文字想换一下呢?再写一个布局include么?显然不能,只能自定义这个空间了。

步骤

<1>把布局写出来,自己想加东西就加点东西

<?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="60dp"
   android:paddingTop="15dp"
   android:paddingBottom="15dp"
   android:background="#FFFCCB4B">
   <ImageView
       android:id="@+id/titleui_left"
       android:layout_width="20dp"
       android:layout_height="20dp"
       android:layout_alignParentLeft="true"
       android:layout_centerVertical="true"
       android:layout_marginLeft="25dp"
       />
   <TextView
       android:id="@+id/titleui_tv"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:textColor="#2b2b2b"
       android:textSize="25sp"
       android:layout_centerInParent="true"
       />
   <ImageView
       android:id="@+id/titleui_right"
       android:layout_width="20dp"
       android:layout_height="20dp"
       android:layout_alignParentRight="true"
       android:layout_centerVertical="true"
       android:layout_marginRight="25dp"
       />
</RelativeLayout>

<2>自定义控件,重写三个构造

titleUi RelativeLayout {

    ImageView ImageView TextView String =String titleUi(Context context) {
       (context)}

    titleUi(Context contextAttributeSet attrs) {
        (contextattrs)}

    titleUi(Context contextAttributeSet attrsdefStyleAttr) {
        (contextattrsdefStyleAttr)

<3>在第三个构造中把布局添加到控件中,addView()这个方法就是干这个事的

titleUi(Context contextAttributeSet attrsdefStyleAttr) {
    (contextattrsdefStyleAttr)View inflate = View.(contextR.layout.)addView(inflate)= (ImageView) findViewById(R.id.)= (ImageView)findViewById(R.id.)= (TextView)findViewById(R.id.)getAttrs(attrs)}

<4>然后我们想到要在代码中设置图标的样式,怎们做呢?在Values中新建attrs,然后自定义属性,

非常简单,我这里就简单的定义了几个属性,你也可以定义很多,比如backgroud,textcolor等等,

<declare-styleable name="titleUi">
<attr name="p_w_picpathleft" format="reference"/>
<attr name="p_w_picpathright" format="reference"/>
<attr name="titletext" format="string"/>
</declare-styleable>

<5>定义了属性就要使用这个属性了,在布局文件中直接使用,比如这样

<naiweiyin.zhiyun.com.myapplication.View.UI.titleUi
   android:id="@+id/tianjiaqianzaixueyuan_bar"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   app:titletext="添加潜在学员"
   app:p_w_picpathleft="@mipmap/fanhui"
   app:p_w_picpathright="@mipmap/yes">
</naiweiyin.zhiyun.com.myapplication.View.UI.titleUi>

<6>然后这样还不行,要在代码中拿到我们自定义的属性才可以真正的使用,不然没有用,像这样就可以拿到我们的属性了,然后把它设置给我们的p_w_picpathView和TextView。

(AttributeSet attrs) {
    = attrs.getAttributeValue()= attrs.getAttributeResourceValue(R.mipmap.)= attrs.getAttributeResourceValue().setText().setImageResource().setImageResource()}

<7>然后我们就要在代码中设置图标的点击事件,和text的内容了,提供三个方法完成这个事情,这样就可以在activity中更具业务设置了

(OnClickListener onclick){
    .setOnClickListener(onclick)}
(OnClickListener onclick){
    (==){
        .setOnClickListener()}{
        .setOnClickListener(onclick)}
}
(String s){
    .setText(s)}

很简单的一个控件就写好了,我们还可以加很多东西来提高它的扩展性,不过一般这样的一个标题栏也时足够用了。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI