View default attribute values of a specific view

Source: Internet
Author: User

When analyzing the focus and touch event processing code, I found that some attributes have a very important impact on the Code logic, such as clickable and focusable.

These attributes. What are the default values of these attributes? I have the same questions many times at work. I was not

It is clear that the settings are basically set manually in XML. There are still many people like me. Today I will tell you how to quickly view these default values through the android source code.

For example, the textview we often use is probably not clickable, that is, the default value of clickable is false. Next, we use

Source code to see if it is like this. First, let's look at the ctor of its two parameters. The Code is as follows:

    public TextView(Context context, AttributeSet attrs) {        this(context, attrs, com.android.internal.R.attr.textViewStyle);    }

The Code shows that the version of the three parameters is actually called, and the 3rd parameters are given to com. Android. Internal. R. ATTR. textviewstyle. Next let's go

Take a look at the system's attrs. xml and find the code similar to this:

<!-- Default TextView style. --><attr name="textViewStyle" format="reference" />

The specific values of these attributes are set in the system's themes. xml file. For details, see:

<style name="Theme"><item name="textViewStyle">@android:style/Widget.TextView</item></style><style name="Theme.Holo"><item name="textViewStyle">@android:style/Widget.Holo.TextView</item></style><style name="Theme.Holo.Light"><item name="textViewStyle">@android:style/Widget.Holo.Light.TextView</item></style>

Then let's go to the styles. xml file to see how these styles are set. The Code is as follows:

    <style name="Widget.TextView">        <item name="android:textAppearance">?android:attr/textAppearanceSmall</item>        <item name="android:textSelectHandleLeft">?android:attr/textSelectHandleLeft</item>        <item name="android:textSelectHandleRight">?android:attr/textSelectHandleRight</item>        <item name="android:textSelectHandle">?android:attr/textSelectHandle</item>        <item name="android:textEditPasteWindowLayout">?android:attr/textEditPasteWindowLayout</item>        <item name="android:textEditNoPasteWindowLayout">?android:attr/textEditNoPasteWindowLayout</item>        <item name="android:textEditSidePasteWindowLayout">?android:attr/textEditSidePasteWindowLayout</item>        <item name="android:textEditSideNoPasteWindowLayout">?android:attr/textEditSideNoPasteWindowLayout</item>        <item name="android:textEditSuggestionItemLayout">?android:attr/textEditSuggestionItemLayout</item>        <item name="android:textCursorDrawable">?android:attr/textCursorDrawable</item>    </style>

These are the default property values of textview. The unspecified clickable and focusable values are false. In the same way, let's see how the buttons of the textview subclass are set:

    <style name="Widget.Button">        <item name="android:background">@android:drawable/btn_default</item>        <item name="android:focusable">true</item>        <item name="android:clickable">true</item>        <item name="android:textAppearance">?android:attr/textAppearanceSmallInverse</item>        <item name="android:textColor">@android:color/primary_text_light</item>        <item name="android:gravity">center_vertical|center_horizontal</item>    </style>

We can see that the default focusable and clickable values of the button are true, and other common attributes such as background, textcolor, and gravity are also set here.

Finally, let's take a look at the edittext Code as follows:

    <style name="Widget.EditText">        <item name="android:focusable">true</item>        <item name="android:focusableInTouchMode">true</item>        <item name="android:clickable">true</item>        <item name="android:background">?android:attr/editTextBackground</item>        <item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item>        <item name="android:textColor">?android:attr/editTextColor</item>        <item name="android:gravity">center_vertical</item>    </style>

The focusableintouchmode of a button is also set to true by default. This ensures that the edittext can obtain the focus even in touch mode, which can be clearly classified by the user.

Which control is receiving input.

This article is a little trick in the development process. You can easily know the default attribute values of any android view through the method described here. Last recommended Article

Yuan You's article on getting custom styles & attribute values: http://www.cnblogs.com/angeldevil/p/3479431.html.

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.