Android 사용자 정의 컨트롤 속성 TypedArray 및 attrs

최근 안 드 로 이 드 사용자 정의 컨트롤 속성 을 연구 하면 서 TypedArray 와 attrs 를 배 웠 습 니 다.여러분 도<안 드 로 이 드 의 사용자 정의 속성 이해>이 글 을 결합 하여 공부 할 수 있 고 후속 한 편 은 응용 할 수 있 습 니 다.
1.attrs 파일 작성

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
 
 <attr name="titleText" format="string" /> 
 <attr name="titleTextColor" format="color" /> 
 <attr name="titleTextSize" format="dimension" /> 
 
 <declare-styleable name="AuthCodeView"> 
 <attr name="titleText" /> 
 <attr name="titleTextColor" /> 
 <attr name="titleTextSize" /> 
 </declare-styleable> 
 
</resources> 
이 위의 코드 에 세 가지 속성 이 있 는 것 을 보 았 습 니 다.먼저 attr 라벨 은 이름과 속성 을 정의 합 니 다.다음은 declare-styleable 그룹 입 니 다.이 그룹의 이름 은 AuthCodeView 이 고 뒤의 class 에서 사 용 됩 니 다.
2.xml 에서 어떻게 인용 하고 사용 하 는 지,시스템 공간 속성 비교
먼저 두 장의 그림 을 보면 반 은 알 수 있 고 반 은 이해 할 수 있다.
a.사용자 정의 속성 이름 참조

b.그림 의 설명 과 a 와 b 그림 의 비 교 를 자세히 보 세 요.속성 명 이 바 뀌 고 어떻게 인용 하 는 지 알 수 있 습 니 다.

위의 그림 이 잘 보이 지 않 을 까 봐 xml 코드 를 첨부 합 니 다.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 xmlns:authcodeview="http://schemas.android.com/apk/res/com.example.authcodeview" 
 android:id="@+id/LinearLayout1" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" > 
 
 <LinearLayout 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" > 
 
 <com.example.authcodeview.view.AuthCodeView 
  android:id="@+id/AuthCodeView" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:padding="10dp" 
  authcodeview:titleText="3712" 
  authcodeview:titleTextColor="#00ffff" 
  authcodeview:titleTextSize="40sp" /> 
 
 <TextView 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="     ,   " /> 
 </LinearLayout> 
 
 <LinearLayout 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" > 
 
 <TextView 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="     " /> 
 
 <EditText 
  android:id="@+id/editText1" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:ems="10" 
  android:inputType="number" > 
 
  <requestFocus /> 
 </EditText> 
 </LinearLayout> 
 
 <Button 
 android:id="@+id/button1" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:text="  " /> 
 
</LinearLayout> 
머리 레이아웃 에 중점 을 두 고 xmlns:android="http://schemas.android.com/apk/res/android"시스템 속성 을 인용 하 는 역할 입 니 다."
그러나 xmlns:authcodeview="http://schemas.android.com/apk/res/com.example.authcodeview"사용자 정의 속성 을 참조 합 니 다."
 xmlns:+이름="http://schemas.android.com/apk/res/ +적용 되 는 패키지 이름"
뒤에 사용 할 때 사용자 정의 속성 이 이 렇 습 니 다.
  • authcodeview:titleText="3712"
  • authcodeview:titleTextColor="#00ffff"
  • authcodeview:titleTextSize="40sp"
  • 시스템 arrs 사용자 정의 경 로 를 추가 합 니 다:

    3.사용자 정의 컨트롤 에서 class 가 문 제 를 어떻게 인용 합 니까?
    코드 를 보고 먼저...
    
    /** 
     *             
     * 
     * @param context 
     * @param attrs 
     * @param defStyle 
     */ 
    public AuthCodeView(Context context, AttributeSet attrs, int defStyle) 
    { 
     super(context, attrs, defStyle); 
     /** 
     *                 
     */ 
     TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.AuthCodeView, defStyle, 0); 
     
     //   attr   ,   AuthCodeView declare-styleable      
     int n = a.getIndexCount(); 
     for (int i = 0; i < n; i++) 
     { 
     int attr = a.getIndex(i); 
     switch (attr) 
     { 
     //        ,         
     case R.styleable.AuthCodeView_titleText: 
      mTitleText = a.getString(attr); 
      break; 
     case R.styleable.AuthCodeView_titleTextColor: 
      //           
      mTitleTextColor = a.getColor(attr, Color.BLACK); 
      break; 
     case R.styleable.AuthCodeView_titleTextSize: 
      //      16sp,TypeValue    sp   px 
      mTitleTextSize = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension( 
       TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics())); 
      break; 
     
     } 
     
     } 
     a.recycle(); 
     } 
    
    이 TypedArray 의 역할 은 바로 자원 의 매 핑 작용 입 니 다.쓰기 방법 은 이 렇 습 니 다.R.styleable.Auth Code View 이거 익숙 하지 않 아 요?
    그리고 R.styleable.AuthCodeViewtitleText,뒤쪽 은 이름 에 밑줄 에 속성 을 추가 합 니 다.
    이렇게 하면 사용자 정의 속성 을 xml 설정 값 에 class 에 표시 합 니 다.어떻게 가 져 오 든 간단 합 니 다.
    이 편 은 여기 서 끝 납 니 다.그리고 이 속편,사용자 정의 속성 컨트롤 도 사용자 정의 view 입 니 다.랜 덤 인증 코드 demo 학습 상세 한 내용 은 보 세 요'안 드 로 이 드 사용자 정의 컨트롤 은 안 드 로 이 드 생 성 랜 덤 인증 코드 를 깊이 학습 합 니 다'

    좋은 웹페이지 즐겨찾기