为了账号安全,请及时绑定邮箱和手机立即绑定

自定义view?

自定义view?

负手_睥睨天下 2016-08-11 17:46:20
package com.sb.testzdyview;import android.annotation.SuppressLint;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.ViewGroup;import android.widget.Button;import android.widget.RelativeLayout;import android.widget.TextView;  @SuppressLint("NewApi") public class Topbar extends RelativeLayout { private Button leftButton,rightButton; private TextView tvTitle; private LayoutParams leftParam,rightParam,titleParam; private int leftTextColor; private Drawable leftBackgroud; private String leftText; private int rightTextColor; private Drawable rightBackgroud; private String rightText; private float titleTextSize; private int titleTextColor; private String title;     @SuppressLint("NewApi") public Topbar(Context context, AttributeSet attrs) { super(context, attrs); // 得到atts中的自定义属相并赋给ta集合 TypedArray ta=context.obtainStyledAttributes(attrs, R.styleable.Topbar);    //相当于初始化atts中的属性,在下面的方法中有两个默认的参数常用到,index,defvalue:默认值 leftTextColor=ta.getColor(R.styleable.Topbar_leftTextColor, 0); leftBackgroud=ta.getDrawable(R.styleable.Topbar_leftBackgroud); leftText=ta.getString(R.styleable.Topbar_leftText); rightTextColor=ta.getColor(R.styleable.Topbar_leftTextColor, 0); rightBackgroud=ta.getDrawable(R.styleable.Topbar_leftBackgroud); 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(leftBackgroud); leftButton.setText(leftText); rightButton.setTextColor(rightTextColor); rightButton.setBackground(rightBackgroud); rightButton.setText(rightText); tvTitle.setTextSize(titleTextSize); tvTitle.setTextColor(titleTextColor); tvTitle.setText(title); //设置tvTitlt居中 tvTitle.setGravity(Gravity.CENTER); //给viewgroup添加背景色 setBackgroundColor(0xfff59563); //定义leftParam的狂傲属性 leftParam=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); //给leftParam增加一个规则,在viewGroup中居左对齐 leftParam.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE); //把leftButton加入到leftParam中 addView(leftButton,leftParam); rightParam=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); //给leftParam增加一个规则,在viewGroup中居左对齐 rightParam.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE); //把leftButton加入到leftParam中 addView(rightButton,rightParam); titleParam=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); titleParam.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE); addView(tvTitle,titleParam); }}
查看完整描述

1 回答

?
ziom

TA贡献948条经验 获得超1109个赞

错误是什么?

查看完整回答
反对 回复 2016-08-11
  • 1 回答
  • 0 关注
  • 1162 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信