美文网首页
自定义view--- titleview

自定义view--- titleview

作者: 璇_c2be | 来源:发表于2019-03-18 19:21 被阅读0次

最近项目中需要大量自定义view.比方说所有界面都有一个title,这个title一般包括一个返回图标,一个title文字,一个右边的button(可有可没有).下图所示:
首先

   private RelativeLayout rl;
    private ImageButton imageButton_left;
    private TextView textView_title;
    private TextView button_right;
   private TitleOnClickListener titleOnClickListener;
    public TitleView(Context context) {
        super(context);
        initView(context);
    }

    public TitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        initAttrs(context,attrs);
    }
private void initView(Context context){
        LayoutInflater.from(context).inflate(R.layout.view_title, this, true);
        rl=findViewById(R.id.title_view_rl);
        imageButton_left=findViewById(R.id.button_left);
        textView_title=findViewById(R.id.title_textView);
        button_right=findViewById(R.id.button_right);
        divider=findViewById(R.id.divider);
    }
    private void initAttrs(Context context,AttributeSet attrs){
        /**获取属性值*/
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.titleView);
        /**标题相关*/
        title_background_color = typedArray.getColor(R.styleable.titleView_title_background, Color.WHITE);
        title_text = typedArray.getString(R.styleable.titleView_title_text);
        title_textColor = typedArray.getColor(R.styleable.titleView_title_textColor, Color.BLACK);
        title_textSize = typedArray.getDimensionPixelSize(R.styleable.titleView_title_textSize, 22);
        /**左按钮相关*/
        left_button_imageId = typedArray.getResourceId(R.styleable.titleView_left_button_image, R.drawable.icon_back);
        left_button_text = typedArray.getString(R.styleable.titleView_left_button_text);
        left_button_textColor = typedArray.getColor(R.styleable.titleView_left_button_textColor, Color.WHITE);
        left_button_textSize = typedArray.getDimensionPixelSize(R.styleable.titleView_left_button_textSize, 20);
        show_left_button = typedArray.getBoolean(R.styleable.titleView_show_left_button, true);
        /**右边保存按钮相关*/
        right_button_imageId = typedArray.getResourceId(R.styleable.titleView_right_button_image, 0);
        right_button_text = typedArray.getString(R.styleable.titleView_right_button_text);
        right_button_textColor = typedArray.getColor(R.styleable.titleView_right_button_textColor, Color.WHITE);
        right_button_textSize = typedArray.getDimensionPixelSize(R.styleable.titleView_right_button_textSize, 20);
        show_right_button = typedArray.getBoolean(R.styleable.titleView_show_right_button, true);
        showDivider = typedArray.getBoolean(R.styleable.titleView_show_divider, true);
        divider.setVisibility(showDivider?View.VISIBLE:View.INVISIBLE);


        setTitle_background_color(title_background_color);
        setTitle_text(title_text);
        setTitle_textSize(title_textSize);
        setTitle_textColor(title_textColor);
        setRight_button_text(right_button_text);
        setShow_left_button(show_left_button);
        setShow_right_button(show_right_button);
        imageButton_left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (titleOnClickListener != null) {
                    titleOnClickListener.onLeftClick();
                }
            }
        });
        button_right.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (titleOnClickListener != null) {
                    titleOnClickListener.onRightClick();
                }
            }
        });
    }
/**
     * 设置标题的点击监听
     *
     * @param titleOnClickListener
     */
    public void setOnTitleClickListener(TitleOnClickListener titleOnClickListener) {
        this.titleOnClickListener = titleOnClickListener;
    }

    /**
     * 监听标题点击接口
     */
    public interface TitleOnClickListener {
        /**
         * 返回按钮的点击事件
         */
        void onLeftClick();

        /**
         * 保存按钮的点击事件
         */
        void onRightClick();

    }

第二步:在attrs.xml添加

<!--自定义 titileview-->
    <declare-styleable name="titleView">
        <attr name="title_background" format="color"/>
        <attr name="left_button_image" format="reference|integer"/>
        <attr name="left_button_text" format="string"/>
        <attr name="left_button_textColor" format="color"/>
        <attr name="left_button_textSize" format="dimension"/>
        <attr name="show_left_button" format="boolean"/>
        <attr name="title_text" format="string"/>
        <attr name="title_textColor" format="color"/>
        <attr name="title_textSize" format="dimension"/>
        <attr name="right_button_image" format="reference|integer"/>
        <attr name="right_button_text" format="string"/>
        <attr name="right_button_textColor" format="color"/>
        <attr name="right_button_textSize" format="dimension"/>
        <attr name="show_right_button" format="boolean"/>
        <attr name="show_divider" format="boolean"/>
    </declare-styleable>

第三步,在.xml里面使用

    <xxx.xxx.xxx.TitleView
        android:id="@+id/titleview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:title_text = "@string/rx_patient_record"
        app:title_textSize = "@dimen/rx_thinking_textsize_title"
        app:show_right_button = "true"
        app:right_button_text = "@string/rx_pattint_zhenliao"
        >
    </xxx.xxx.xxx.TitleView>

第四部

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/title_view_rl"
    android:layout_width="match_parent"
    android:background="@color/colorWhite"
    android:layout_height="@dimen/title_height">

    <ImageButton
        android:padding="@dimen/add_patient_margin_left"
        android:layout_centerVertical="true"
        android:layout_margin="@dimen/dp_4"
        android:layout_marginLeft="@dimen/add_patient_margin_left"
        android:background="@color/white"
        android:src="@drawable/icon_back"
        android:id="@+id/button_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

    <TextView
        android:layout_centerInParent="true"
        android:textSize="@dimen/rx_title_text_size"
        android:id="@+id/title_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textColor="@color/rx_text_title_color"
        android:text="@string/rx_setting_agency" />

    <TextView
        android:background="@null"
        android:textSize="@dimen/rx_title_right_text_size"
        android:textColor="@color/rx_primary_color"
        android:paddingLeft="@dimen/add_patient_margin_left"
        android:paddingRight="@dimen/add_patient_margin_left"
        android:layout_alignParentRight="true"
        android:id="@+id/button_right"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="@string/rx_setting_commit"
         />

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:id="@+id/divider"
        android:layout_alignParentBottom="true"
        android:background="@drawable/rx_divider"/>
</RelativeLayout>

相关文章

网友评论

      本文标题:自定义view--- titleview

      本文链接:https://www.haomeiwen.com/subject/wwgbmqtx.html