Providing default style (attributes) in user view

Please let me know how I can set the default background for the desired button.

I mean ... I know that I can define the "style" that android sets: the background is "@null", and ask users to explicitly apply the style in their layout. For example:

<style name="MyButton" parent="@android:style/Widget.Button"> <item name="android:background">@null</item> </style> 

and

 <com.xxx.widget.MyButton android:layout_width="match_parent" android:layout_height="wrap_content" style="@style/MyButton" android:text="MyButton" /> 

The code works well. But how can I apply this style in my "MyButton" class internally and let users not set the style explicitly?

For example, how to make the following layout work as before:

 <com.xxx.widget.MyButton android:layout_width="match_parent" android:layout_height="wrap_content" android:text="MyButton" /> 

I tried to do this in the constructor as shown below, but it doesn't work.

 public MyButton(Context context, AttributeSet attrs) { this(context, attrs, com.xxx.R.style.MyButton); } 

PS. I want to apply this "zero" background when the user does not set the background explicitly.

+7
source share
2 answers

In my constructor, MyButton (), why not call setBackground (null)?

0
source

This is a more or less cheap way to do this, but why not set the background to a transparent square. How is it β†’

transparent_square.xml: (put it in the dropdown directory)

 <?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> 

Then set the button background to your drawable.

Button:

 <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"/> 
0
source

All Articles