Android 在自定义视图中提供默认样式(属性)
请让我知道,如何将自定义按钮的默认背景设置为空 我是说。。。 我知道我可以定义一个“样式”,将android:background设置为“@null”, 并要求用户在布局中明确应用样式。例如:Android 在自定义视图中提供默认样式(属性),android,view,background,widget,styles,Android,View,Background,Widget,Styles,请让我知道,如何将自定义按钮的默认背景设置为空 我是说。。。 我知道我可以定义一个“样式”,将android:background设置为“@null”, 并要求用户在布局中明确应用样式。例如: <style name="MyButton" parent="@android:style/Widget.Button"> <item name="android:background">@null</item> </style> 另外,当用户未设
<style name="MyButton" parent="@android:style/Widget.Button">
<item name="android:background">@null</item>
</style>
另外,当用户未设置背景时,我希望应用此“空”背景
明确地说。在MyButton()构造函数中,为什么不调用setBackground(null)?这或多或少是一种便宜的方法,但为什么不将背景设置为透明的正方形呢。像这样-> transparent_square.xml:(将其放在可绘制目录中)
//前两个零设置alpha
//到0
然后将按钮的背景设置为可绘制的
按钮:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:id="@+id/sendButton"
android:layout_weight="1"
android:background="@drawable/transparent_square"/>
我认为您应该更改问题的标题。您可以始终使用默认样式,但以后可以更改。也许你需要说“最终风格”或类似的话,请参阅有关这方面的一些提示。你也有类似的要求:抱歉,但我认为这并不能回答问题@Henry询问在默认情况下如何做到这一点,而不在按钮的xml中指定。
<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);
}
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/square"
android:>
<solid android:color="#00808080"></solid> // The first two zeros set the alpha
// to 0
</shape>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:id="@+id/sendButton"
android:layout_weight="1"
android:background="@drawable/transparent_square"/>