2017-02-10 39 views
-1

我穿过所有给定的构造样式参考:如何在不使用样式属性的情况下设置自定义视图的样式?

public MyCustomView(Context context) 
{ 
    super(context, null, R.style.MyStyle); 
} 
public MyCustomView(Context context, AttributeSet attrs) 
{ 
    super(context, attrs, R.style.MyStyle); 
} 
public MyCustomView(Context context, AttributeSet attrs, int defStyle) 
{ 
    super(context, attrs, R.style.MyStyle); 
} 

我的棉花糖尝试此。样式不适用,但在通过样式属性应用时起作用。

+0

据我所知,有4个不是3视图的构造函数 – Selvin

+0

是不是假设是'super'而不是'this' –

回答

0

尝试使用三个参数的构造函数与ContextThemeWrapper如下:

MyCustomView mView = new MyCustomView(new ContextThemeWrapper(this,R.style.yourStyle),null,0); 

当你MyCustomView类的构造函数如下:

public MyCustomView(Context context, AttributeSet attrs, int defStyle) 
{ 
    super(context, attrs, defStyle); 
} 

这应该工作。

+0

但是如何通过xml来实现呢? –

+0

您可以通过为其分配自定义样式来覆盖您的_Activity_或_Application's_ _theme_属性,并将其应用于整个_Activity_或_Application_,但是如果您想在xml中设置单个视图的样式,那么这是我认为您可以通过xml(通过设置样式属性)。而且这也是有道理的,没有人需要通过其他方式来做这样的事情。 [你应该在这里详细研究](https://developer.android.com/guide/topics/ui/themes.html) –

相关问题