请告诉我如何将自定义按钮的默认背景设置为null。
我的意思是... 我知道我可以定义一个“style”,将android:background设置为“@null”,并要求用户在其布局中明确应用该样式。例如:
例如,如何使以下布局像以前一样工作:
我的意思是... 我知道我可以定义一个“style”,将android:background设置为“@null”,并要求用户在其布局中明确应用该样式。例如:
<style name="MyButton" parent="@android:style/Widget.Button">
<item name="android:background">@null</item>
</style>
和
<com.xxx.widget.MyButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/MyButton"
android:text="MyButton" />
上述代码运行良好。但是我该如何在我的类"MyButton"内部应用这种样式,而让用户不必显式地设置样式呢?例如,如何使以下布局像以前一样工作:
<com.xxx.widget.MyButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="MyButton" />
我尝试在构造函数中按如下方式执行此操作,但它不起作用。
public MyButton(Context context, AttributeSet attrs) {
this(context, attrs, com.xxx.R.style.MyButton);
}
附注:我希望在用户没有明确设置背景时应用这个"null"背景。