亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

自定義View 中無法設置背景色問題

在自定義代碼中加入 setBackgroundColor(0xF70C2F); 不起作用? 在xml文件中使用android:background="#FF4571"也不起作用。控件背景色一直都是白色的。哪位大哥知道怎么回事呀?

正在回答

4 回答

遇到了同樣的問題,請問樓主是怎么解決的,一直沒找到原因

0 回復 有任何疑惑可以回復我~

使用ContextCompat.getColor()方法獲取顏色,比如setBackground(ContextCompat.getColor(context,leftBtnbackground))

0 回復 有任何疑惑可以回復我~
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);這句話刪除的話就可以了。

實在搞不懂是怎么回事?

1 回復 有任何疑惑可以回復我~
#1

王修念

你要設置?setBackgroundColor(0xFFED280E) 注意區別。
2016-04-08 回復 有任何疑惑可以回復我~

你把代碼放上來看看

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
Android UI模板設計
  • 參與學習       76032    人
  • 解答問題       233    個

快來學習如何在Android中自定義View,本次課程一定會讓你獲益匪淺

進入課程

自定義View 中無法設置背景色問題

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號