Is it possible to have custom attributes in Androi

2019-01-21 19:39发布

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

5条回答
虎瘦雄心在
2楼-- · 2019-01-21 19:50

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

查看更多
欢心
3楼-- · 2019-01-21 19:56

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

查看更多
孤傲高冷的网名
4楼-- · 2019-01-21 20:04

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

查看更多
三岁会撩人
5楼-- · 2019-01-21 20:13

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")
查看更多
何必那么认真
6楼-- · 2019-01-21 20:14

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.

查看更多
登录 后发表回答