Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Currently the best documentation is the source. You can take a look at it <a href="https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/attrs.xml" rel="noreferrer">here (attrs.xml)</a>. </p> <p>You can define attributes in the top <code>&lt;resources&gt;</code> element or inside of a <code>&lt;declare-styleable&gt;</code> element. If I'm going to use an attr in more than one place I put it in the root element. Note, all attributes share the same global namespace. That means that even if you create a new attribute inside of a <code>&lt;declare-styleable&gt;</code> element it can be used outside of it and you cannot create another attribute with the same name of a different type.</p> <p>An <code>&lt;attr&gt;</code> element has two xml attributes <code>name</code> and <code>format</code>. <code>name</code> lets you call it something and this is how you end up referring to it in code, e.g., <code>R.attr.my_attribute</code>. The <code>format</code> attribute can have different values depending on the 'type' of attribute you want. </p> <ul> <li>reference - if it references another resource id (e.g, "@color/my_color", "@layout/my_layout")</li> <li>color</li> <li>boolean</li> <li>dimension</li> <li>float</li> <li>integer</li> <li>string</li> <li>fraction</li> <li>enum - normally implicitly defined</li> <li>flag - normally implicitly defined</li> </ul> <p>You can set the format to multiple types by using <code>|</code>, e.g., <code>format="reference|color"</code>.</p> <p><code>enum</code> attributes can be defined as follows:</p> <pre><code>&lt;attr name="my_enum_attr"&gt; &lt;enum name="value1" value="1" /&gt; &lt;enum name="value2" value="2" /&gt; &lt;/attr&gt; </code></pre> <p><code>flag</code> attributes are similar except the values need to be defined so they can be bit ored together:</p> <pre><code>&lt;attr name="my_flag_attr"&gt; &lt;flag name="fuzzy" value="0x01" /&gt; &lt;flag name="cold" value="0x02" /&gt; &lt;/attr&gt; </code></pre> <p>In addition to attributes there is the <code>&lt;declare-styleable&gt;</code> element. This allows you to define attributes a custom view can use. You do this by specifying an <code>&lt;attr&gt;</code> element, if it was previously defined you do not specify the <code>format</code>. If you wish to reuse an android attr, for example, android:gravity, then you can do that in the <code>name</code>, as follows.</p> <p>An example of a custom view <code>&lt;declare-styleable&gt;</code>:</p> <pre><code>&lt;declare-styleable name="MyCustomView"&gt; &lt;attr name="my_custom_attribute" /&gt; &lt;attr name="android:gravity" /&gt; &lt;/declare-styleable&gt; </code></pre> <p>When defining your custom attributes in XML on your custom view you need to do a few things. First you must declare a namespace to find your attributes. You do this on the root layout element. Normally there is only <code>xmlns:android="http://schemas.android.com/apk/res/android"</code>. You must now also add <code>xmlns:whatever="http://schemas.android.com/apk/res-auto"</code>.</p> <p>Example:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:whatever="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;org.example.mypackage.MyCustomView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" whatever:my_custom_attribute="Hello, world!" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>Finally, to access that custom attribute you normally do so in the constructor of your custom view as follows.</p> <pre><code>public MyCustomView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView, defStyle, 0); String str = a.getString(R.styleable.MyCustomView_my_custom_attribute); //do something with str a.recycle(); } </code></pre> <p>The end. :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload