自定义view的实现方式为三种
一种:对现有的控件进行拓展(比如继承tTextView 然后通过重写onDraw方法在里面改造)
二种:通过组合来实现新控件(如下面的demo)
三种:重写view来实现全新的控件(下章节在记录)
通过组合来实现新控件之创建一个通用标题栏view
第一步:一般标题栏都分为左右按钮,和中间标题栏,定义这些控件的属性 在value文件夹下面新建一个attrs的资源文件 如下
<?xml version="1.0" encoding="utf-8"?> <resources> <!--declare-styleable申明自定义属性 --> <!--reference是引用类型--> <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="leftText" format="string" /> <attr name="leftBackground" format="reference|color" /> <attr name="rightTextColor" format="color" /> <attr name="rightText" format="string" /> <attr name="rightBackground" format="reference|color" /> </declare-styleable> </resources>
第二步 创建一个view继承自一个viewgroup
//通过组合来形成 public class MyRegroupView extends RelativeLayout { private String title; private int titleTextColor; private float titleTextSize; private String leftText; private Drawable leftBackground; private int leftColor; private String rightText; private Drawable rightBackground; private int rightColor; private Button leftButton; private Button rightButton; private TextView textView; private LayoutParams layoutParams; public MyRegroupView(Context context) { super(context); } public MyRegroupView(Context context, AttributeSet attrs) { super(context, attrs); setXmlProperty(context, attrs); initView(context); } public MyRegroupView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); setXmlProperty(context, attrs); initView(context); } private void setXmlProperty(Context context, AttributeSet attrs) { // 通过这个方法将attrs中定义的declare-styleable的所有属性值存储到TypedArray中 TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TopBar); title = ta.getString(R.styleable.TopBar_title); titleTextColor = ta.getColor(R.styleable.TopBar_titleTextColor, 0); titleTextSize = ta.getDimension(R.styleable.TopBar_titleTextSize, 14); leftText = ta.getString(R.styleable.TopBar_leftText); leftBackground = ta.getDrawable(R.styleable.TopBar_leftBackground); leftColor = ta.getColor(R.styleable.TopBar_leftTextColor, 0); rightText = ta.getString(R.styleable.TopBar_rightText); rightBackground = ta.getDrawable(R.styleable.TopBar_rightBackground); rightColor = ta.getColor(R.styleable.TopBar_rightTextColor, 0); // 获取完TypedArray的值后,调用recycle方法来避免重复创建时候的错误 ta.recycle(); } /** * 这个是创建这个标题栏包含的控件,并赋值 */ private void initView(Context context) { leftButton = new Button(context); rightButton = new Button(context); textView = new TextView(context); // 为创建的组建元素赋值 // 值来源于来源于xml赋值 leftButton.setText(leftText); leftButton.setBackground(leftBackground); leftButton.setTextColor(leftColor); rightButton.setText(rightText); rightButton.setBackground(rightBackground); rightButton.setTextColor(rightColor); textView.setText(title); textView.setTextSize(titleTextSize); textView.setTextColor(titleTextColor); // 添加左布局 addView(RelativeLayout.ALIGN_PARENT_LEFT, leftButton); // 添加右布局 addView(RelativeLayout.ALIGN_PARENT_RIGHT, rightButton); // 添加中间标题栏 addView(RelativeLayout.CENTER_IN_PARENT, textView); } /** * 将视图添加到这个viewgroup中 * @param addRule * @param view */ private void addView(int addRule, View view) { layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT); layoutParams.addRule(addRule, TRUE); addView(view, layoutParams); } /** * 通过接口回调的方式,把点击事件给回传会去 * @param topbarOnclickListener */ public void setTopbarOnclickListener(final TopbarOnclickListener topbarOnclickListener) { // 给左右按钮添加点击事件 leftButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { topbarOnclickListener.leftClick(); } }); rightButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { topbarOnclickListener.rightClick(); } }); } // 定义左右两个按钮的点击事件接口 public interface TopbarOnclickListener { void leftClick(); void rightClick(); } // 设置按钮是否显示 public void setButtonVisable(int id, boolean flag) { if (flag) { if (id == 0) { leftButton.setVisibility(View.VISIBLE); } else { rightButton.setVisibility(View.VISIBLE); } } else { if (id == 0) { leftButton.setVisibility(View.GONE); } else { rightButton.setVisibility(View.GONE); } } } }
第三步:在activity中使用
public class MainActivity extends AppCompatActivity { private MyRegroupView myRegroupView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myRegroupView = ((MyRegroupView) findViewById(R.id.myRegroupView)); myRegroupView.setTopbarOnclickListener(new MyRegroupView.TopbarOnclickListener() { @Override public void leftClick() { Toast.makeText(MainActivity.this,"点击了左按钮",Toast.LENGTH_SHORT).show(); } @Override public void rightClick() { Toast.makeText(MainActivity.this,"点击了右按钮",Toast.LENGTH_SHORT).show(); } }); } }
布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!--注意xmlns定义的xml的命名空间改变了--> <com.lvqueen.qunyinzhuan.day01.MyRegroupView android:id="@+id/myRegroupView" android:layout_width="match_parent" android:layout_height="60dp" custom:leftText="左边按钮" custom:leftTextColor="#000000" custom:leftBackground="@mipmap/ic_launcher" custom:title="这个是标题" custom:titleTextSize="18sp" custom:titleTextColor="#000000" custom:rightTextColor="#000000" custom:rightText="右边按钮" custom:rightBackground="@mipmap/ic_launcher" /> </LinearLayout>