Is it possible to have custom attributes in Androi

2019-01-21 19:16发布

问题:

I would like to add a custom attribute to the application tag of my AndroidManifest.xml file. Is this possible in the Android environment?

回答1:

Yes. Here's an example. The custom tag is ContentVersion.

<application android:name=".MyApplication"
             android:icon="@drawable/icon"
             android:label="@string/app_name">

    <meta-data android:name="ContentVersion" android:value="1.9" />

    <activity android:name="com.someone.something.MainActivity"
              android:theme="@android:style/Theme.Translucent.NoTitleBar"
              android:screenOrientation="sensor"
              android:label="@string/app_name">

To access it:

    ApplicationInfo ai = _context.getPackageManager().getApplicationInfo(_context.getPackageName(),PackageManager.GET_META_DATA);
    ai.metaData.get("ContentVersion")


回答2:

You cannot define custom attribute to a predefined tag, but you can add key-value pairs called meta-data.



回答3:

You could go for a SharedPreferences, instead (aka, settings).



回答4:

In the tag (as well as service and receiver), you can use the tag (http://developer.android.com/guide/topics/manifest/meta-data-element.html )

It contains a name and a value or a resource ID.

You retrieve it through the PackageManager.



回答5:

If anybody needs that for Xamarin (Mono for Android) I couldn't find the constant, but i found the value for it, which is 128.

I used a "for" condition to go through all values from 0 to 1000 and check whenever the MetaData property was not null. lol