自定義View 中無法設置背景色問題
在自定義代碼中加入 setBackgroundColor(0xF70C2F); 不起作用? 在xml文件中使用android:background="#FF4571"也不起作用??丶尘吧恢倍际前咨?。哪位大哥知道怎么回事呀?
在自定義代碼中加入 setBackgroundColor(0xF70C2F); 不起作用? 在xml文件中使用android:background="#FF4571"也不起作用??丶尘吧恢倍际前咨?。哪位大哥知道怎么回事呀?
2015-12-23
舉報
2019-04-07
遇到了同樣的問題,請問樓主是怎么解決的,一直沒找到原因
2016-05-25
使用ContextCompat.getColor()方法獲取顏色,比如setBackground(ContextCompat.getColor(context,leftBtnbackground))
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); ????} }?我試過了
在代碼中使用setBackgroundColor(0xED280E); 控件得背景色并沒用換色,并且在xml文件中使用android:background="@color/colorAccent"設置背景色也不起作用,如果把setBackgroundColor(0xED280E);這句話刪除的話就可以了。
實在搞不懂是怎么回事?
2015-12-23
你把代碼放上來看看