美文网首页
Android 自定义组合控件

Android 自定义组合控件

作者: 百里漫步 | 来源:发表于2017-10-26 16:30 被阅读0次

这里是自定义标题栏控件
1、首先建立一个resource文件最为控件属性配置
atts.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="Topbar">
        <attr name="title" format="string"/>
        <attr name="titleTextSize" format="dimension"></attr>
        <attr name="titleTextColor" format="color"></attr>
        <attr name="leftText" format="string"/>
        <attr name="leftTextColor" format="color"></attr>
        <attr name="leftBackground" format="reference|color"></attr>
        <attr name="rightText" format="string"/>
        <attr name="rightTextColor" format="color"></attr>
        <attr name="rightBackground" format="reference|color"></attr> 
    </declare-styleable>
</resources>

2、然后继承RelativeLayout类,实现组合自定义控件和方法,添加自定义回调接口
Topbar.java

package com.example.mydemo;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Topbar extends RelativeLayout {

    private Button leftButton, rightButton;
    private TextView title;

    private String titleText;
    private float titleTextSize;
    private int titleTextColor;

    private String leftButtonText;
    private Drawable leftBackground;
    private int leftTextColor;

    private String rightButtonText;
    private Drawable rightBackground;
    private int rightTextColor;

    private LayoutParams leftParams, rightParams, titleParams;

    private OnTopBarClickListener listener;

    interface OnTopBarClickListener {
        public void leftClick();

        public void rightClick();
    }

    void setOnTopBarClick(OnTopBarClickListener listener) {
        this.listener = listener;
    }

    public Topbar(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray ta = context.obtainStyledAttributes(attrs,
                R.styleable.Topbar);
        titleText = ta.getString(R.styleable.Topbar_title);
        titleTextColor = ta.getColor(R.styleable.Topbar_titleTextColor, 0);
        titleTextSize = ta.getDimension(R.styleable.Topbar_titleTextSize, 0);

        leftButtonText = ta.getString(R.styleable.Topbar_leftText);
        leftBackground = ta.getDrawable(R.styleable.Topbar_leftBackground);
        leftTextColor = ta.getColor(R.styleable.Topbar_leftTextColor, 0);

        rightButtonText = ta.getString(R.styleable.Topbar_rightText);
        rightBackground = ta.getDrawable(R.styleable.Topbar_rightBackground);
        rightTextColor = ta.getColor(R.styleable.Topbar_rightTextColor, 0);

        //setBackgroundColor(0xFFF59563);
        // 回收资源
        ta.recycle();

        title = new TextView(context);
        leftButton = new Button(context);
        rightButton = new Button(context);
        // 属性设置
        title.setText(titleText);
        title.setTextColor(titleTextColor);
        title.setTextSize(titleTextSize);

        leftButton.setText(leftButtonText);
        leftButton.setBackground(leftBackground);
        leftButton.setTextColor(leftTextColor);

        rightButton.setText(rightButtonText);
        rightButton.setBackground(rightBackground);
        rightButton.setTextColor(rightTextColor);

        // 宽高设置
        leftParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        // 位置规则设置
        leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
        addView(leftButton, leftParams);

        rightParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
        addView(rightButton, rightParams);

        titleParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.MATCH_PARENT);
        titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
        titleParams.setMargins(0, 20, 0, 0);
        addView(title, titleParams);

        //接口回调
        leftButton.setOnClickListener(new OnClickListener() {       
            @Override
            public void onClick(View arg0) {
                //让调用者去实现我们自己定义的接口
                listener.leftClick();
            }
        });
        rightButton.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                listener.rightClick();
                
            }
        });
    }

}

然后再我们需要的布局上引入我们的自定义控件,像这样
xmlns:app="http://schemas.android.com/apk/res/com.example.mydemo"
就可以用了 。
rela.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="match_parent"
    xmlns:app="http://schemas.android.com/apk/res/com.example.mydemo">
    
    <com.example.mydemo.Topbar 
        android:id="@+id/topbar"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="#ff0000"
        app:title="标题"
        app:titleTextSize="10sp"
        app:titleTextColor="#ffffff"
        app:leftText="返回"
        app:leftTextColor="#ffffff"
        app:leftBackground="#000000"
        app:rightText="前进"
        app:rightTextColor="#ffffff"
        app:rightBackground="#000000"
        
       >
      
        
        
    </com.example.mydemo.Topbar>

</RelativeLayout>

MainActivity.java

package com.example.mydemo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

import com.example.mydemo.Topbar.OnTopBarClickListener;

public class MainActivity extends Activity {

    private Topbar tb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rela);
        tb = (Topbar) findViewById(R.id.topbar);
        tb.setOnTopBarClick(new OnTopBarClickListener() {
            
            @Override
            public void rightClick() {
                Toast.makeText(MainActivity.this, "前进", Toast.LENGTH_SHORT).show();
                
            }
            
            @Override
            public void leftClick() {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, "后退", Toast.LENGTH_SHORT).show();
            }
        });
    }


    

}

演示效果:


topbar.gif

相关文章

  • Android自定义控件之自定义组合控件

    Android自定义控件之自定义组合控件 前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原...

  • 【Android】自定义控件

    Android自定义控件可以有三种实现方式 组合原生控件自己绘制控件继承原生控件 1 组合原生控件 1.1 组合原...

  • Android中的自定义控件

    Android中的自定义控件大致可以分成三类:自定义组合控件、继承原生控件的自定义控件、继承View自己实现绘制的...

  • 自定义的控件简介

    android 自定义控件简介 安卓中的自定义控件可以分为三种: 通过将系统提供的控件组合,成为新的控件 自定义V...

  • 自定义控件的原因以及动画的分类

    什么是自定义控件? 在 Android 系统中使用系统自带控件重新组合或者自定义类继承 View / ViewGr...

  • 组合自定义控件的步骤详解

    Android 步骤: 1 自定义组合控件的布局settint_view.xml 2 创建一个自定义子和控件的类S...

  • 自定义控件概述

    阅读原文 1.自定义控件的一些概念 1.什么是自定义控件 在Android系统中,用系统的自带控件重新组合或者自定...

  • Android自定义控件(一,基本原理)

    自定义控件相关目录: Android自定义控件(一,基本原理) Android自定义控件(二,自定义属性) And...

  • Android入门06 -- 自定义控件

    自定义组合控件 将几个子控件组合在一起,形成一个可复用的新的组合控件,自定义组合控件一般继承自RelativeLa...

  • Android开发之自定义View(一)

    Android常见的自定义控件有三种方式: 继承View 继承原有的控件,在原有控件的基础上进行修改 重新拼装组合...

网友评论

      本文标题:Android 自定义组合控件

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