2013-09-16 26 views
17

我希望我的ViewA和ViewB都有「標題」標籤。但我不能把這個attrs.xml如何爲不同的標籤聲明幾個具有相同名稱的stylable屬性?

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="ViewA"> 
     <attr name="title" format="string" /> 
    </declare-styleable> 
    <declare-styleable name="ViewB"> 
     <attr name="title" format="string" /> 
     <attr name="max" format="integer" /> 
    </declare-styleable> 
</resources> 

因爲錯誤屬性「稱號」已被定義Another question示出了該解決方案:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <attr name="title" format="string" /> 
    <declare-styleable name="ViewB"> 
     <attr name="max" format="integer" /> 
    </declare-styleable> 
</resources> 

但是在這種情況下,不產生R.styleable.ViewA_titleR.styleable.ViewB_title。我需要它們使用以下代碼從AttributeSet中讀取屬性:

TypedArray a=getContext().obtainStyledAttributes(as, R.styleable.ViewA); 
String title = a.getString(R.styleable.ViewA_title); 

我該如何解決這個問題?

+0

類似於http://stackoverflow.com/questions/4434327/same-named-attributes-in-attrs-xml-for-custom-view – Suragch

回答

32

你已經張貼不給你正確的答案的鏈接。這是它表明你做的事:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <attr name="title" format="string" /> 
    <declare-styleable name="ViewA"> 
     <attr name="title" /> 
    </declare-styleable> 
    <declare-styleable name="ViewB"> 
     <attr name="title" /> 
     <attr name="max" format="integer" /> 
    </declare-styleable> 
</resources> 

現在,R.styleable.ViewA_titleR.styleable.ViewB_title都可以訪問。

如果您有機會,請通讀此答案:Link。相關報價:

您可以在元素的頂部元素或內部定義屬性。如果我打算在更多的 中使用attr,而不是將它放在根元素中。

+0

這似乎不再成立(或至少在使用Android Studio 2.3時)。當我在根中定義屬性時,出現錯誤,指出這些資源無法解析。我認爲你需要定義一個具有共享屬性的父項。 – BioeJD

+0

現在不適用於Android Studio。 – user3269713

+0

截至2018-2的正確答案是https://stackoverflow.com/a/43635002/1963973 – marianosimone

3

您需要使用繼承

<resources> 
    <declare-styleable name="ViewA"> 
     <attr name="title" format="string" /> 
    </declare-styleable> 

    <declare-styleable name="ViewB" parent="ViewA"> // inherit from ViewA 
     <attr name="min" format="integer" /> 
     <attr name="max" format="integer" /> 
    </declare-styleable> 
</resources> 

在Java代碼中

String namespace = "http://schemas.android.com/apk/res/" + getContext().getPackageName(); 
int title_resource = attrs.getAttributeResourceValue(namespace, "title", 0); 
String title = ""; 
if(title_resource!=0){ 
    title = getContext().getString(title_resource); 
} 

int min = attrs.getAttributeResourceValue(namespace, "min", 0); // read int value 
int max = attrs.getAttributeResourceValue(namespace, "max", 0); 
+0

這是行不通的。它不會生成ViewB_title。 ViewA_title不能用於ViewB元素,因爲其ID將與ViewB_min衝突。 – Andreas

2

取而代之。沒有parent標籤需要

<resources> 
    <declare-styleable name="ViewA"> 
     <attr name="title" format="string" /> 
    </declare-styleable> 

    <declare-styleable name="ViewB" > 
     <attr name="title" /> 
     <attr name="min" format="integer" /> 
     <attr name="max" format="integer" /> 
    </declare-styleable> 
</resources> 

這是因爲一旦titleViewA聲明,它不需要(也不能)被再次在另一個declare-styleable

1
<resources> 
<declare-styleable name="ViewA"> 
    <attr name="title" format="string" /> 
</declare-styleable> 

<declare-styleable name="ViewB" parent="ViewA"> // inherit from ViewA 
    <attr name="min" format="integer" /> 
    <attr name="max" format="integer" /> 
</declare-styleable> 

聲明

這不起作用

<resources> 
<attr name="title" format="string" /> 
<declare-styleable name="ViewA"> 
    <attr name="title" /> 
</declare-styleable> 
<declare-styleable name="ViewB"> 
    <attr name="title" /> 
    <attr name="max" format="integer" /> 
</declare-styleable> 

這也不起作用。

<resources> 
<declare-styleable name="ViewA"> 
    <attr name="title" format="string" /> 
</declare-styleable> 

<declare-styleable name="ViewB" > 
    <attr name="title" /> 
    <attr name="min" format="integer" /> 
    <attr name="max" format="integer" /> 
</declare-styleable> 

這是OK!

相關問題