自定义view?

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);

}


}


负手_睥睨天下
浏览 1165回答 1
1回答

ziom

错误是什么?
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android