android 自定义viewgroup addrule无效,自定义View 中无法设置背景色问题

package com.im.imdemo.view;

import android.content.Context;

import android.content.res.TypedArray;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.ColorFilter;

import android.graphics.drawable.ColorDrawable;

import android.graphics.drawable.Drawable;

import android.text.Layout;

import android.util.AttributeSet;

import android.view.Gravity;

import android.view.ViewGroup;

import android.widget.Button;

import android.widget.RelativeLayout;

import android.widget.TextView;

import com.im.imdemo.R;

/**

* Created by Administrator on 2015/12/23.

*/

public class CustomTopBar extends RelativeLayout {

private Button btn_left,btn_right;

private TextView tv_title;

private String titleText;

private float titleTextSize;

private int titleTextColor;

private String leftBtnText;

private int leftBtnColor;

private Drawable leftBtnbackground;

private String rightBtnText;

private int rightBtnColor;

private Drawable rightBtnbackground;

private LayoutParams leftParams,rightParams,titleParams;

public CustomTopBar(Context context) {

super(context);

}

public CustomTopBar(Context context, AttributeSet attrs) {

super(context, attrs);

InitView(context, attrs);

}

private  void InitView(Context context, AttributeSet attributeSet){

TypedArray ta = context.obtainStyledAttributes(attributeSet, R.styleable.CustomTopBar);

titleText = ta.getString(R.styleable.CustomTopBar_topBarText);

titleTextColor = ta.getColor(R.styleable.CustomTopBar_topBarTextColor, 0);

titleTextSize = ta.getDimension(R.styleable.CustomTopBar_topBarTextSize, 0);

leftBtnText =  ta.getString(R.styleable.CustomTopBar_leftBtnText);

leftBtnColor = ta.getColor(R.styleable.CustomTopBar_leftBtnTextColor, 0);

leftBtnbackground = ta.getDrawable(R.styleable.CustomTopBar_leftBackground);

rightBtnText =  ta.getString(R.styleable.CustomTopBar_rightBtnText);

rightBtnColor = ta.getColor(R.styleable.CustomTopBar_rightBtnTextColor, 0);

rightBtnbackground = ta.getDrawable(R.styleable.CustomTopBar_rightBackground);

ta.recycle(); //释放TypedArray 资源

setBackgroundColor(0xED280E);

//实例化View

btn_left = new Button(context);

btn_right = new Button(context);

tv_title = new TextView(context);

//设置View属性

btn_left.setText(leftBtnText);

btn_left.setTextColor(leftBtnColor);

btn_left.setBackground(leftBtnbackground);

btn_right.setText(rightBtnText);

btn_right.setTextColor(rightBtnColor);

btn_right.setBackground(rightBtnbackground);

tv_title.setText(titleText);

tv_title.setTextColor(titleTextColor);

tv_title.setTextSize(titleTextSize);

tv_title.setGravity(Gravity.CENTER);

//设置View的布局

leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);

addView(btn_left, leftParams);

rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);

addView(btn_right, rightParams);

titleParams = new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);

addView(tv_title,titleParams);

}

}<?xml  version="1.0" encoding="utf-8"?>

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.im.imdemo.ui.ContentActivity">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="@color/colorAccent"

custom:topBarText="Hello"

custom:topBarTextSize="10sp"

custom:topBarTextColor="#0A0909"

custom:leftBackground="@color/colorAccent"

custom:leftBtnText="返回"

custom:leftBtnTextColor="@color/blue"

custom:rightBtnText="添加"

custom:rightBtnTextColor="@color/blue"

custom:rightBackground="@color/colorAccent"

android:layout_alignParentRight="true">

我试过了

在代码中使用setBackgroundColor(0xED280E); 控件得背景色并没用换色,并且在xml文件中使用android:background="@color/colorAccent"设置背景色也不起作用,如果把setBackgroundColor(0xED280E);这句话删除的话就可以了。

实在搞不懂是怎么回事?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值