簡體   English   中英

Android 自定義屬性未顯示

[英]Android custom attributes not showing

我看過很多帖子,有人想知道如何為自定義組件獲取自定義屬性,但這不是我的問題。 我創建了一個自定義組件,我正在嘗試添加屬性,但是當我在我的 xml 文件頂部添加命名空間時,它只找到兩個隨機的自定義屬性“paddingEnd”和“paddingStart”。

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

這是 attrs.xml 文件。

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
    }
}

這是我的自定義類中的構造函數。

        <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>

這是我正在使用的組件。 我想向“imageId”和“menuText”添加值,但它們不可用。 唯一顯示的是與填充相關的內容,如下所示。

在此處輸入圖片說明

有什么想法嗎?

找到了解決辦法。

碰巧的是,您需要具有與類名相同的樣式名稱。

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

在這種情況下,關於這個主題的許多帖子都是錯誤的,因為我可以看到很多樣式名稱完全不同。

希望這可以阻止其他人落入這個陷阱。

干杯

我的解決方案是:在 attr.xml 文件中,您可能已按如下方式聲明您的自定義屬性

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

這里, declare-styleable的 name 字段必須是自定義視圖的名稱。 然后它會顯示在建議中。

在我的情況下,我的自定義視圖名稱是IDEditText ,因此可以declare-styleable名稱 =“IDEditText”

雖然這篇文章有點舊,但為了提供信息,我將分享我的案例。

我定義了一個名為attrs.xml的自定義屬性文件以與 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>

我使用拖動 attr 使用此選擇器更改 cardview 的筆觸顏色:

<?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>

最后我有一個名為DeviceMaterialCardView的自定義類。 現在自定義屬性與自定義類同名。 但是我在編譯him_stroke_color xml 文件時遇到編譯錯誤:

/home/Projeler/PK-BLE-RF/Android/Uygulama/PK0101BLE/app/src/main/res/color/him_stroke_color.xml:6: AAPT: 錯誤: 屬性 com.himtec.himtec_kontrol:state_dragging 未找到。

我仍然被卡住,無法弄清楚如何解決這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM