Android Custom Properties set the global background for the application

Source: Internet
Author: User

With regard to custom attributes, we use a lot more time to customize the view, in fact, the custom attribute has some other magical uses.

Here is an example of using a custom attribute to replace the background of the application globally. 1.Android instances of using custom attributes

Maybe we've seen this way many times in the use of toolbar.

<android.support.v7.widget.Toolbar    style="@style/ToolBarStyle"xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="?attr/actionBarSize"    android:background="?attr/colorPrimary" />

When determining the width and background color of the toolbar, it does not write in the XML file, but instead reads the Google custom attribute values.
We would be curious, so how can we get the value, where is this attribute defined, and about how to get the value is another problem, and now say where this value is defined, and where to assign the initial value.
First find the theme of our current project, the parent of my current project theme is Theme.AppCompat.Light.DarkActionBar, find its parent, click inside, this is inside Android res\ Some of the themes, attributes, and so on within values.
Search for Actionbarsize and colorprimary this property.

And then look at the declare-styleable name of this property.

Well, I don't really need to know Declare-styleable's name, it doesn't matter.

Here we know that the values of Android do have the definition of these properties, not just imagined, and then have a definition of what we must assign to these properties before using it, it is expected that developers may not know about it. There must be an assignment in values, and we'll keep looking.

From where to find out, of course, from our theme of the parent to find out, said before the current program is the subject of inheritance Theme.AppCompat.Light.DarkActionBar.
We're going to find the corresponding assignment operation. The final discovery theme is inherited Theme.light, of course, also inherit a lot of layers, you can see for yourself.

You can see that this is an assignment to the colorprimary.
Actionbarsize's assignment is in Base.V7.Theme.AppCompat.Light.

So here's some of the knowledge we know that if we want to do something like android:background= "? Attr/colorprimary" in XM, it's very simple to use some of the custom properties, just three steps.

Steps:

The first step, define the properties second step, assign a value to the custom property in the third step, use the custom property value 2. An example of a custom attribute

Now there is a need for this, the current program in order to unify the style, all the interface needs a unified background or background color, and this background map or back color can be freely customized, that is, the style file can be freely configured. This time the custom attribute is very convenient to complete this effect.
Three steps:

2.1. Define Custom properties

Create a new Attrs.xml file inside values that defines a custom attribute.

Code is also posted here:

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="App_Attrs">        <!-- 默认背景图 -->        <attr name="baseBackground" format="color|reference"/>    </declare-styleable></resources>
2.2. Assigning a value to a custom property

We need to assign a value to this property in the theme of the current program.

<resources> <stylename="Apptheme"Parent="Theme.AppCompat.Light.DarkActionBar"> <Item name="Basebackground"> @android: color/holo_green_light</Item> <Item name="Colorprimary"> @color/colorprimary</Item> <Item name="Colorprimarydark"> @color/colorprimarydark</Item> <Item name="Coloraccent"> @color/coloraccent</Item> </style></resources>

The assignment is complete.

2.3. Using a custom property value

Since all the interfaces of our current program need to use this background color, we can define a baseactivity of our own, which is a simple process for setcontentview.

2.3.1. Creating an XML layout for base_layout

Base_layout.xml

<?xml version="1.0" encoding="utf-8"?><FrameLayout    android:id="@+id/base_layout_container"xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="?attr/baseBackground"></FrameLayout>

A custom property value is referenced inside.

2.3.2.baseActivity of code:
 Public  class baseactivity extends appcompatactivity {Framelayout Mbasecontainerview;@Override     Public void Setcontentview(@LayoutResintLAYOUTRESID) {Setcontentview (Getlayoutinflater (). Inflate (Layoutresid,NULL)); }@Override     Public void Setcontentview(View view) {Setcontentview (view,NewViewgroup.layoutparams (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); }@Override     Public void Setcontentview(View view, Viewgroup.layoutparams params) {Mbasecontainerview = (framelayout) getlayoutinflater (). Inflate (R.layout.base_layout,NULL); drawable bg = View.getbackground ();if(BG! =NULL) {if(Build.VERSION.SDK_INT >= build.version_codes.                Jelly_bean) {Findviewbyid (R.id.base_layout_container). SetBackground (BG); View.setbackground (NULL); }Else{Findviewbyid (R.id.base_layout_container). setbackgrounddrawable (BG); View.setbackgrounddrawable (NULL); }} framelayout.layoutparams P =NewFramelayout.layoutparams (FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT); Mbasecontainerview.addview (view, p);Super. Setcontentview (Mbasecontainerview, params); }}

The Setcontentview method is simply handled in the code.

2.3.3.MainActivity can be used directly to see the background color has changed
publicclass MainActivity extends BaseActivity {    @Override    protectedvoidonCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }}

There is a picture of the truth:

Demo Address: http://download.csdn.net/detail/qy274770068/9499356

Android Custom Properties set the global background for the application

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.