Android 사용자 정의 컨트롤 속성 TypedArray 및 attrs
6192 단어 android사용자 정의 컨트롤TypedArrayattrs
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/ +적용 되 는 패키지 이름"
뒤에 사용 할 때 사용자 정의 속성 이 이 렇 습 니 다.
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 학습 상세 한 내용 은 보 세 요'안 드 로 이 드 사용자 정의 컨트롤 은 안 드 로 이 드 생 성 랜 덤 인증 코드 를 깊이 학습 합 니 다'
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin의 기초 - 2부지난 글에서는 Kotlin이 무엇인지, Kotlin의 특징, Kotlin에서 변수 및 데이터 유형을 선언하는 방법과 같은 Kotlin의 기본 개념에 대해 배웠습니다. 유형 변환은 데이터 변수의 한 유형을 다른 데이터...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.