Use styles in Android

Source: Internet
Author: User

Attrs. xml defines attributes:
If I define the control mycustomwidget myself, in Main. XML, I want to use this control and set the property Android: textsize =. How can I set this?

Where does this property come from? You need to set attrs. XML, create such a file in the values directory, and then the content is as follows:
<Resources>

<Declare-styleable name = "mycustomview">

<ATTR name = "text" format = "string"/>

<ATTR name = "textcolor" format = "color"/>

Qisda changer Tel: 6029 write the paper.

<ATTR name = "textsize" format = "dimension"/>

</Declare-styleable>

</Resources>

What is the value type of some attribute names?

Well, now that we have this property type, we can set the property in layout.

Xmlns: Android = "http://schemas.android.com/apk/res/android"

Add one sentence below.

Xmlns: myview = "http://schemas.android.com/apk/res/com.ui"

Myview is the namespace. you can name it at will. com. UI is the name of the package that declares the control property.

With this, you should understand why the control attributes are set to Android.
<Com. UI. mycustomview

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Qisda changer write the paper.

Myview: text = "@ string/app_name"
Myview: textsize = "32dp"
Myview: textcolor = "@ drawable/yellow"/>

In this way, you can set attributes.
Styles usage:

Styles. XML is used to define a set of attribute values. Format.
<? XML version = "1.0" encoding = "UTF-8"?>

<Resources>

<Style name = "mycustomview">

<Item name = "textcolor"> # ffff0000 </item>

Qisda changer write the paper.

<Item name = "textsize"> 60dp </item>

</Style>

</Resources>
Styles is essentially a set of attribute values.

In this way, the style can be referenced by the style name in layout. For example, style = "@ style/mycustomview.

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.