简体   繁体   中英

Android custom attributes not showing

I've seen a lot of posts with people wanting to know how to get custom attributes for a custom component but that is not my question. I have created a custom component and I'm trying to add attributes but when I add the namespace at the top of my xml file it only finds two random custom attributes "paddingEnd" and "paddingStart".

<resources>
    <declare-styleable name="menu_item_attrs">
        <attr name="imageId" format="integer" />
        <attr name="menuText" format="string" />
    </declare-styleable>
</resources>

This is attrs.xml file.

public MenuListItem(Context context, AttributeSet set) {
    super(context, set);

    TypedArray a = context.obtainStyledAttributes(set, R.styleable.menu_item_attrs);
    if (a == null) {
        return;
    }
    CharSequence s = a.getString(R.styleable.menu_item_attrs_menuText);
    if (s != null) {
        // do something
    }
}

This is the constructor in my custom class.

        <LinearLayout
            xmlns:custom="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:id="@+id/expanding_layout"
            android:background="#029eed">

            <aaron.testappanim.MenuListItem
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"/>
        </LinearLayout>

This is my component in use. I want to add values to "imageId" and "menuText" but they aren't available. The only things that show are padding related as shown below.

在此处输入图片说明

Any ideas guys?

Found the solution.

As it happens you need to have the styleable name the same as your class name.

<resources>
    <declare-styleable name="MenuListItem">
        <attr name="my_custom_attribute" format="integer" />
    </declare-styleable>
</resources>

Many many posts on this subject are wrong in that case as I can see plenty with the styles name being completely different.

Hopefully this can stop someone else falling into this trap.

Cheers

My solution was: In the attr.xml file, you may have declared your custom attribute as follows

<declare-styleable name="IDEditText"> <attr name="customFont" format="string"/> </declare-styleable>

Here, the name field of declare-styleable must be the name of the custom view. Then it will show in the suggestions.

In mycase my custom view name is IDEditText , hence the declare-styleable name="IDEditText"

Although this post is a bit old, just for the sake of information I will share my case.

I defined a custom attribute file named attrs.xml to use with MaterialCardView:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="DeviceMaterialCardView">
        <attr name="state_none" format="boolean"/>
        <attr name="state_favorite" format="boolean"/>
        <attr name="state_dragging" format="boolean"/>
    </declare-styleable>
</resources>

I use dragging attr to change the stroke color of the cardview with this selector:

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:himtec="http://schemas.android.com/apk/res-auto">

    <item android:color="?attr/colorSecondary" himtec:durum_surukle="true"/>
    <item android:color="?attr/colorSecondary" android:state_activated="true"/>
    <item android:alpha="0.2" android:color="?attr/colorOnBackground"/>
</selector>

And finally I have the custom class named DeviceMaterialCardView . Now the custom attribute has the same name as the custom class. But yet I am getting compilation error while compiling the him_stroke_color xml file:

/home/Projeler/PK-BLE-RF/Android/Uygulama/PK0101BLE/app/src/main/res/color/him_stroke_color.xml:6: AAPT: error: attribute com.himtec.himtec_kontrol:state_dragging not found.

I'm still stuck and can't figure out how to get around this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM