自定义View 中无法设置背景色问题
在自定义代码中加入 setBackgroundColor(0xF70C2F); 不起作用 在xml文件中使用android:background="#FF4571"也不起作用。控件背景色一直都是白色的。哪位大哥知道怎么回事呀?
在自定义代码中加入 setBackgroundColor(0xF70C2F); 不起作用 在xml文件中使用android:background="#FF4571"也不起作用。控件背景色一直都是白色的。哪位大哥知道怎么回事呀?
2015-12-23
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"?> <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.im.imdemo.ui.ContentActivity"> <com.im.imdemo.view.CustomTopBar 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"> </com.im.imdemo.view.CustomTopBar> </RelativeLayout>
我试过了
在代码中使用setBackgroundColor(0xED280E); 控件得背景色并没用换色,并且在xml文件中使用android:background="@color/colorAccent"设置背景色也不起作用,如果把setBackgroundColor(0xED280E);这句话删除的话就可以了。
实在搞不懂是怎么回事?
举报