【发布时间】:2012-05-21 08:33:56
【问题描述】:
请告诉我,如何将自定义按钮的默认背景设置为 null。
我的意思是... 我知道我可以定义一个“样式”,将 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);
}
PS。当用户未设置背景时,我想应用此“空”背景 明确的。
【问题讨论】:
-
我认为您应该更改问题的标题。您始终可以使用默认样式,但以后可以更改。也许你需要说“最终风格”或类似的东西
-
请参阅stackoverflow.com/questions/4493947/… 了解有关此方面的一些提示。
标签: android view background widget styles