Android 组合控件之标题栏

本文介绍了如何使用XML布局和Java代码结合的方式来自定义Android应用中的标题栏组件,并提供了完整的实现代码及示例。

1、闲聊

上一篇博客我们已经聊了聊组合控件了,这里我就不瞎BB了,没有看过的,可以去看一下Android 组合控件之删除文本框

http://blog.youkuaiyun.com/ty_phone8/article/details/77916268

2、实例

说不BB就不BB,来我们会看另一种组合控件的实现代码:

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="wrap_content"
    android:background="#dcdcdc"
    android:gravity="center"
    android:orientation="horizontal">

    <RelativeLayout
        android:id="@+id/customcontrol_title_bar_left_rl"
        android:layout_width="44dp"
        android:layout_height="44dp"
        android:gravity="center">

        <ImageView
            android:id="@+id/customcontrol_title_bar_left_iv"
            android:layout_width="22dp"
            android:layout_height="22dp"
            android:src="@drawable/custom_control_back" />

    </RelativeLayout>

    <TextView
        android:id="@+id/customcontrol_title_bar_center_tv"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="This is a title."
        android:textColor="#6976bb"
        android:textSize="18sp" />

    <RelativeLayout
        android:id="@+id/customcontrol_title_bar_right_rl"
        android:layout_width="44dp"
        android:layout_height="44dp"
        android:gravity="center">

        <ImageView
            android:id="@+id/customcontrol_title_bar_right_iv"
            android:layout_width="22dp"
            android:layout_height="22dp"
            android:src="@drawable/custom_control_plus" />

    </RelativeLayout>

</LinearLayout>

自定义:
public class TitleBar extends RelativeLayout {
    private View titleBar;
    private RelativeLayout leftGroup;
    private ImageView leftImg;
    private TextView centerTxt;
    private RelativeLayout rightGroup;
    private ImageView rightImg;

    public TitleBar(Context context) {
        super(context);
        init(context);
    }

    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public TitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public TitleBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private <T extends View> T findTitleBarById(int resId) {
        return (T) titleBar.findViewById(resId);
    }

    private void init(Context context) {
        titleBar = LayoutInflater.from(context).inflate(R.layout.customcontrol_titlebar, null);
        leftGroup = findTitleBarById(R.id.customcontrol_title_bar_left_rl);
        leftImg = findTitleBarById(R.id.customcontrol_title_bar_left_iv);
        centerTxt = findTitleBarById(R.id.customcontrol_title_bar_center_tv);
        rightGroup = findTitleBarById(R.id.customcontrol_title_bar_right_rl);
        rightImg = findTitleBarById(R.id.customcontrol_title_bar_right_iv);
        leftGroup.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != onClickTitleBarListener) {
                    onClickTitleBarListener.onClickLeft(v);
                }
            }
        });
        rightGroup.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != onClickTitleBarListener) {
                    onClickTitleBarListener.onClickRight(v);
                }
            }
        });
        centerTxt.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != onClickTitleBarListener) {
                    onClickTitleBarListener.onClickTitle(v);
                }
            }
        });

        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        titleBar.setLayoutParams(lp);

        addView(titleBar);
    }

    public void setTitleBarLeftImage(Object obj) throws RuntimeException {
        if (null != leftImg) {
            if (obj instanceof Integer) {
                leftImg.setImageResource((Integer) obj);
            } else if (obj instanceof Bitmap) {
                leftImg.setImageBitmap((Bitmap) obj);
            } else if (obj instanceof Drawable) {
                leftImg.setImageDrawable((Drawable) obj);
            } else if (obj instanceof Uri) {
                leftImg.setImageURI((Uri) obj);
            } else {
                throw new RuntimeException("Please pass the parameters correctly.");
            }
        } else {
            throw new RuntimeException("The control was not found.");
        }
    }

    public void setTitleBarRightImage(Object obj) throws RuntimeException {
        if (null != rightImg) {
            if (obj instanceof Integer) {
                rightImg.setImageResource((Integer) obj);
            } else if (obj instanceof Bitmap) {
                rightImg.setImageBitmap((Bitmap) obj);
            } else if (obj instanceof Drawable) {
                rightImg.setImageDrawable((Drawable) obj);
            } else if (obj instanceof Uri) {
                rightImg.setImageURI((Uri) obj);
            } else {
                throw new RuntimeException("Please pass the parameters correctly.");
            }
        } else {
            throw new RuntimeException("The control was not found.");
        }
    }

    public void setTitleBarCenterText(String text) throws RuntimeException {
        if (null != centerTxt) {
            centerTxt.setText(text);
        } else {
            throw new RuntimeException("The control was not found.");
        }
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public void setTitleBarBackground(Object obj) throws RuntimeException {
        if (null != titleBar) {
            if (obj instanceof Integer) {
                titleBar.setBackgroundResource((Integer) obj);
            } else if (obj instanceof Drawable) {
                titleBar.setBackground((Drawable) obj);
            } else {
                throw new RuntimeException("Please pass the parameters correctly.");
            }
        } else {
            throw new RuntimeException("The control was not found.");
        }
    }

    public ImageView getLeftImg() {
        return leftImg;
    }

    public TextView getCenterTxt() {
        return centerTxt;
    }

    public ImageView getRightImg() {
        return rightImg;
    }

    private onClickTitleBarListener onClickTitleBarListener;

    public void setOnClickTitleBarListener(TitleBar.onClickTitleBarListener onClickTitleBarListener) {
        this.onClickTitleBarListener = onClickTitleBarListener;
    }

    public interface onClickTitleBarListener {
        void onClickLeft(View v);

        void onClickRight(View v);

        void onClickTitle(View v);
    }
}

demo调用:
TitleBar titleBar = (TitleBar) findViewById(R.id.titleBar);
        titleBar.setTitleBarCenterText("标题");
        titleBar.setTitleBarLeftImage(R.drawable.custom_control_plus);
        titleBar.setTitleBarRightImage(R.drawable.custom_control_back);
        titleBar.setTitleBarBackground(android.R.color.holo_blue_light);
        titleBar.setOnClickTitleBarListener(new TitleBar.onClickTitleBarListener() {
            @Override
            public void onClickLeft(View v) {
                toask_make("This is the picture on the left.");
            }

            @Override
            public void onClickRight(View v) {
                toask_make("Here is the picture on the right.");
            }

            @Override
            public void onClickTitle(View v) {
                toask_make("This is the title section.");
            }
        });

3、demo资源

开源地址:

https://github.com/fountaintao/CustomControl


组合控件我们就聊到这里,两种方法都介绍了一下,删除文本框我们使用的是纯代码,这个组合控件我们使用的是xml + java实现


欢迎大家指正、分享

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值