自定义View

来源:互联网 发布:淘宝粉星便利店靠谱么 编辑:程序博客网 时间:2024/05/01 12:19

我们改怎么做呢?

1,设计需要的属性
建立atts.xml文件

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="Topbar">        <attr name="titlea" format="string" />        <attr name="titleColor" format="color" />        <attr name="titleTextSize" format="dimension" />        <attr name="leftBackgroud" format="reference|color" />        <attr name="leftTextColor" format="color" />        <attr name="leftText" format="string" />        <attr name="rightBackgroud" format="reference|color" />        <attr name="rightTextColor" format="color" />        <attr name="rightText" format="string" />    </declare-styleable></resources>

2,设计一个我们的View,继承Relativelayout

package com.zhong.topbar;import android.content.Context;import android.content.res.TypedArray;import android.graphics.drawable.Drawable;import android.util.AttributeSet;import android.view.Gravity;import android.view.View;import android.view.ViewGroup;import android.view.WindowManager;import android.widget.Button;import android.widget.RelativeLayout;import android.widget.TextView;import android.widget.Toast;/** * Created by Vctor on 2016/2/16. */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 int titleColor;    private float titleTextSize;    private String titlea;    public topbarClickListener listener;    private LayoutParams leftParams,rightParams,titleParams;    public interface topbarClickListener{        public void leftClick();        public void rightClick();    }    public void setOnTopbarClickListener(topbarClickListener listener){        this.listener = listener;    }    public Topbar(final 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_leftBackgroud);        leftText = ta.getString(R.styleable.Topbar_leftText);        rightTextColor = ta.getColor(R.styleable.Topbar_rightTextColor,0);        rightBackground = ta.getDrawable(R.styleable.Topbar_rightBackgroud);        rightText = ta.getString(R.styleable.Topbar_rightText);        titleTextSize = ta.getDimension(R.styleable.Topbar_titleTextSize,0);        titleColor = ta.getColor(R.styleable.Topbar_titleColor,0);        titlea = ta.getString(R.styleable.Topbar_titlea);        ta.recycle();        leftButton = new Button(context);        rightButton = new Button(context);        tvTitle = new TextView(context);        rightButton.setTextColor(rightTextColor);        rightButton.setBackground(rightBackground);        rightButton.setText(rightText);        leftButton.setTextColor(leftTextColor);        leftButton.setBackground(leftBackground);        leftButton.setText(leftText);        tvTitle.setTextColor(titleColor);        tvTitle.setTextSize(titleTextSize);        tvTitle.setText(titlea);        tvTitle.setGravity(Gravity.CENTER);        setBackgroundColor(0xFFF59563);        leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);        leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);        addView(leftButton,leftParams);        rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);        rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);        addView(rightButton,rightParams);        titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);        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 setLeftIsVisable(boolean flag){        if (flag){            leftButton.setVisibility(View.VISIBLE);        }else {            leftButton.setVisibility(View.INVISIBLE);        }    }}

3,引用我们的view

activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:custom="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.zhong.topbar.MainActivity">   <com.zhong.topbar.Topbar       android:id="@+id/topbar"       android:layout_width="match_parent"       android:layout_height="40dp"       custom:leftBackgroud="@drawable/blue_button"       custom:leftText="Back"       custom:leftTextColor="#FFFFFF"       custom:rightBackgroud="@drawable/blue_button"       custom:rightText="More"       custom:rightTextColor="#FFFFFF"       custom:titlea="自定义标题"       custom:titleColor="#123421"       custom:titleTextSize="10sp">   </com.zhong.topbar.Topbar></RelativeLayout>

MainActivity.Java

package com.zhong.topbar;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.Toast;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Topbar topbar =(Topbar) findViewById(R.id.topbar);        topbar.setOnTopbarClickListener(new Topbar.topbarClickListener() {            @Override            public void leftClick() {                Toast.makeText(MainActivity.this,"我是左边按钮",Toast.LENGTH_SHORT).show();            }            @Override            public void rightClick() {                Toast.makeText(MainActivity.this,"我是右边按钮",Toast.LENGTH_SHORT).show();            }        });        //设置左边Button不可见        topbar.setLeftIsVisable(false);    }}
0 0
原创粉丝点击