自定义View:标题栏

作者: Jennyni1122 | 来源:发表于2019-09-25 18:56 被阅读0次

    效果图:

    标题栏

    实现步骤:

    1. atts.xml文件定义所需要的属性
    2. 继承组件实现自定义ViewGroup
      (1)获取xml文件定义的属性为typeArray
      (2)再从typeArray提取每一个属性
      (3)给控件设置布局属性LayoutParams和rule
    3. 在布局文件中引入自定义组件(xmlns:.....)

    注意:点击事件:定义接口,创建方法,实现接口与方法的映射(接口回调机制)

    代码实现:

    attr.xml

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

    Topbar.java

    public class Topbar  extends RelativeLayout{
    
        //定义控件
        private Button leftButton,rightButton;
        private TextView tvTitle;
        //定义控件属性
        private int leftTextColor;
        private Drawable leftBackground;
        private String leftText;
    
        private int rightTextColor;
        private Drawable rightBackground;
        private String rightText;
    
        private float titleTextSize;
        private int titleTextColor;
        private String title;
    
        private LayoutParams leftParams,rightParams,titleParams;
    
        private topbarClickListener listener;
    
        public interface topbarClickListener{
            public void leftClick();
            public void rightClick();
        }
    
        public void setOnTopbarClickListener(topbarClickListener listener){
            this.listener = listener;
        }
    
        public Topbar(Context context, AttributeSet attrs) {
            super(context, attrs);
    
            TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.Topbar);
            leftTextColor = ta.getColor(R.styleable.Topbar_leftTextColor,0);
            leftBackground = ta.getDrawable(R.styleable.Topbar_leftBackground);
            leftText = ta.getString(R.styleable.Topbar_leftText);
    
            rightTextColor = ta.getColor(R.styleable.Topbar_rightTextColor,0);
            rightBackground = ta.getDrawable(R.styleable.Topbar_rightBackground);
            rightText = ta.getString(R.styleable.Topbar_rightText);
    
            titleTextSize = ta.getDimension(R.styleable.Topbar_titleTextSize,0);
            titleTextColor = ta.getColor(R.styleable.Topbar_titleTextColor,0);
            title = ta.getString(R.styleable.Topbar_title);
    
            ta.recycle();
    
            //自定义三个控件(组合模式)
            leftButton = new Button(context);
            rightButton = new Button(context);
            tvTitle = new TextView(context);
    
            leftButton.setTextColor(leftTextColor);
            leftButton.setBackground(leftBackground);
            leftButton.setText(leftText);
    
            rightButton.setTextColor(rightTextColor);
            rightButton.setBackground(rightBackground);
            rightButton.setText(rightText);
    
            tvTitle.setTextColor(titleTextColor);
            tvTitle.setTextSize(titleTextSize);
            tvTitle.setText(title);
            tvTitle.setGravity(Gravity.CENTER);
    
            setBackgroundColor(0xFF59653);
    
            //属性设置在LayoutParams
            leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
            leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
            addView(leftButton,leftParams);
    
            rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
            rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
            addView(rightButton,rightParams);
    
            titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
            titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
            addView(tvTitle,titleParams);
    
            leftButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.leftClick();
                }
            });
    
            rightButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.rightClick();
                }
            });
        }
    
        public void setLeftvisible(boolean flag){
            if (flag){
                leftButton.setVisibility(View.VISIBLE);
            }else{
                leftButton.setVisibility(View.GONE);
            }
        }
       
    }
    

    activity_main.xml

      <com.jennyni.testdemo.Topbar
            android:id="@+id/topbar"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            app:leftBackground="@color/colorAccent"
            app:leftText="Back"
            app:leftTextColor="@android:color/black"
            app:rightBackground="@color/colorAccent"
            app:rightText="More"
            app:rightTextColor="@android:color/black"
            app:title="标题"
            app:titleTextSize="10sp"
            app:titleTextColor="@android:color/holo_green_light">
        </com.jennyni.testdemo.Topbar>
    

    MainActivity

    public class MainActivity extends Activity {
    
        private Topbar topbar;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            topbar = findViewById(R.id.topbar);
            topbar.setOnTopbarClickListener(new Topbar.topbarClickListener() {
                @Override
                public void leftClick() {
                    Toast.makeText(MainActivity.this, "LEFT", Toast.LENGTH_SHORT).show();
                }
    
                @Override
                public void rightClick() {
                    Toast.makeText(MainActivity.this, "RIGHT", Toast.LENGTH_SHORT).show();
                }
            });
    
          //左按钮不可见
            topbar.setLeftvisible(false);
        }
    }
    

    显示图:

    显示图

    相关文章

      网友评论

        本文标题:自定义View:标题栏

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