How do you pass the gravity attribute via xml for

2019-07-15 20:32发布

问题:

I'm trying to figure out how to pass android:gravity via xml for a custom view.

The solution posted here (https://stackoverflow.com/a/3441986/413254) says to add the android:gravity attribute to the other attr's.

When I do this, I end up with a warning saying:

/Users/greg/dev/company/mobile/my_app/app/src/main/res/values/colors.xml

Error:Attribute "android:gravity" has already been defined Error:Execution failed for task ':app:processStagingDebugResources'.

com.android.ide.common.internal.LoggedErrorException: Failed to run command: /Users/greg/dev/android-sdk/sdk/build-tools/21.0.2/aapt package -f --no-crunch -I /Users/greg/dev/android-sdk/sdk/platforms/android-21/android.jar -M /Users/greg/dev/company/mobile/myapp/app/build/intermediates/manifests/full/staging/debug/AndroidManifest.xml -S /Users/greg/dev/company/mobile/myapp/app/build/intermediates/res/staging/debug -A /Users/greg/dev/company/mobile/myapp/app/build/intermediates/assets/staging/debug -m -J /Users/greg/dev/company/mobile/myapp/app/build/generated/source/r/staging/debug -F /Users/greg/dev/company/mobile/myapp/app/build/intermediates/res/resources-staging-debug.ap_ --debug-mode --custom-package com.company.vendorreviews -0 apk --output-text-symbols /Users/greg/dev/company/mobile/myapp/app/build/intermediates/symbols/staging/debug Error Code: 1 Output: /Users/greg/dev/company/mobile/myapp/app/build/intermediates/res/staging/debug/values/values.xml:165: error: Attribute "android:gravity" has already been defined

My colors.xml file?

Line 165 of values.xml is

<declare-styleable name="StackedTextView">
    <attr format="integer" name="android:gravity"/>
    <attr format="string" name="line1"/>
    <attr format="string" name="line2"/>
    <attr format="dimension" name="line1_textSize"/>
    <attr format="dimension" name="line2_textSize"/>
</declare-styleable>

There are multiple occurrences of android:gravity in multiple groups. Theme, FlowLayout (library I include), LinearLayoutCompat, etc.

Edit: Updated error message.

回答1:

by writing:

<attr format="integer" name="android:gravity"/>

you are intruducing the new attribute android:gravity with integer format, of course android:gravity is already defined in the system, so you need to change it into:

<attr name="android:gravity"/>