Theme/Style is not applied when inflater used with ApplicationContext

前端 未结 4 1925
挽巷
挽巷 2020-11-28 10:40

I have theme that specifies textColor for TextView as red.

I am using LayoutInflater to instantiate TextView. The problem is that styles are not applied to TextView

4条回答
  •  天命终不由人
    2020-11-28 10:59

    I usually come across this issue on inflating a custom view. Here is what I personally do to keep the same theme of the activity in the CustomView

    public class CustomView extends ViewGroup{
    
    public CustomView (Context context) {
        super(context);
        init(context);
    }
    
    public CustomView (Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }
    
    public CustomView (Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }
    
    @TargetApi(21)
    public CustomView (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }
    
    private void init(Context context) {
        LayoutInflater  mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = mInflater.inflate(R.layout.review_list_item, this, true);
        //rest of view initialization       
    }   
    }
    

提交回复
热议问题