2013-08-29 18 views
5

Zrobiłem mój niestandardowy widok i chcę ustawić dla niego niestandardowe atrybuty. Chcę przekazać identyfikator innego widoku jako atrybut.Widok niestandardowy Android z niestandardowymi atrybutami

Widok niestandardowy attrs:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="IdNumber"> 
     <attr name="firstName" format="integer"/> 
     <attr name="lastName" format="integer"/> 
     <attr name="address" format="integer"/> 
     <attr name="birthDate" format="integer"/> 
    </declare-styleable> 
</resources> 

Układ gdzie mogę używać mojego klienta Widok:

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:custom="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <EditText 
     android:id="@+id/display_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="text" 
     android:layout_centerHorizontal="true" 
     android:tag="name" 
     android:ems="10" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="number" 
     android:ems="10" 
     android:id="@+id/id_number" 
     android:layout_centerHorizontal="true" 
     android:layout_below="@id/display_name"/> 

    <ge.altasoft.custom_views.IdNumber 
     android:id="@+id/custom_id_number" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_below="@id/id_number" 
     android:paddingLeft="35dip" 
     custom:firstName="@id/display_name"/> 


</RelativeLayout> 

konstruktor widok niestandardowy Class, gdzie chcę, aby uzyskać wartość atrybutu:

public IdNumber (Context context, AttributeSet attrs) { 
     super(context, attrs); 
     initViews(); 

     TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IdNumber); 
     final int N = a.getIndexCount(); 
     for(int i = 0; i < N; i++){ 
      int attr = a.getIndex(i); 
      switch(attr){ 
       case R.styleable.IdNumber_firstName: 
        int firstNameViewID = a.getInteger(attr, -1); 
        break; 
      } 
     } 
     a.recycle(); 
    } 

Problem polega na tym, że int firstNameViewID = a.getInteger(attr, -1); to tylko 0, a nie id widoku.

custom:firstName="@id/display_name" < < < tutaj powinno być coś złego, ale nie wiem, co jest z nim nie tak. Kiedy przypisuję do niestandardowego atrybutu jakąś wartość całkowitą, to działa, ale nie działa na Id-s.

Dzięki za pomoc w Advance.

Odpowiedz

12

zmienić określić <attr name="firstName" format="reference"/> iw użytku kodu int firstNameViewID = a.getResourceId(attr, -1);

nadzieję, że to pomaga!

+0

Dzięki, że pomogło. – Jilberta

0

użycie custom:firstName="@+id/display_name" iw swoim styleable go zmienić na <attr name="firstName" format="refernce"/> zamiast całkowitej

+0

Już go użyłem, ale to nie pomoże. – Jilberta

+0

i podczas pobierania identyfikatora użyj a.getReference() –

+1

dzięki za odpowiedź, ale a.getResourceId (attr, -1) pomógł. – Jilberta

Powiązane problemy