美文网首页
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 自定义组合控件

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