Android에서 개발한 사용자 정의 UI 구성 요소 및 속성

안드로이드 시스템은 자체적으로 많은 구성 요소를 가지고 있지만 우리의 개성화된 수요를 충족시킬 수 있기 때문에 우리는 개발을 편리하게 하기 위해 안드로이드의 UI 구성 요소를 사용자 정의하여 우리의 개성화된 수요를 실현해야 한다.조합 컨트롤을 사용자화하려면
1. 상대 레이아웃, 선형 레이아웃 등 ViewGroup의 하위 클래스를 상속하는 뷰를 설정합니다.ViewGroup은 다양한 구성 요소를 곱하여 배치할 수 있는 다른 컨트롤 컨테이너입니다.2. 부류를 실현하는 세 가지 구조 방법.일반적으로 구조 방법에서 초기 사용자 정의 레이아웃 파일을 초기화해야 한다.하나의 매개 변수 구조 방법: new 컨트롤에 두 개의 매개 변수를 사용합니다. 레이아웃 파일을 호출할 때 세 개의 매개 변수를 사용합니다. 스타일을 가진 레이아웃 파일을 전달할 때 3. 필요에 따라 API 방법을 정의합니다.4. 필요에 따라 컨트롤의 속성을 사용자 정의한다.TextView 속성 쓰기를 참조하십시오.5. 네임스페이스를 사용자 정의합니다. xmlns:xxx="http://schemas.android.com/apk/res/<패키지 이름> "xxx는 scheam 이름 6이고 우리의 속성을 사용자 정의합니다.res/values/attrs.xml(속성 파일 만들기)에서 속성을 정의합니다.

<7, 사용자 정의 속성을 사용합니다."8. 사용자 정의 컨트롤의 두 가지 파라미터가 있는 구조 방법에서 AttributeSet attrs는 사용자 정의 속성 값을 꺼내 사용자 정의 레이아웃 파일에 대응하는 컨트롤을 연결합니다. 코드 실례는 다음과 같습니다: 1 사용자 정의 컨트롤을 정의합니다: setting item view.xml 레이아웃 문서

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="68dip" >

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip"
        android:layout_marginTop="8dip"
        android:text="        "
        android:textColor="#000000"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tv_desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_title"
        android:layout_marginLeft="10dip"
        android:text="        "
        android:textColor="#88000000"
        android:textSize="18sp" />

    <CheckBox
        android:id="@+id/cb_status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dip" />

    <View
        android:clickable="false"
        android:layout_width="match_parent"
        android:layout_height="0.2dip"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="5dip"
        android:layout_marginRight="5dip"
        android:background="#000000" />

</RelativeLayout>

2 Activity 레이아웃 파일에서 호출
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:andy="http://schemas.android.com/apk/res/com.andy.mobilesafe"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

   <com.andy.mobilesafe.ui.SettingItemView
        android:id="@+id/siv_update"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        andy:desc_off="          "
        andy:desc_on="          "
        andy:titles="      " />
</LinearLayout>

3 사용자 정의 속성:res/values/attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="TextView">

        <!--          -->
        <attr name="desc_on" format="string" />
        <attr name="desc_off" format="string" />
        <attr name="titles" format="string" />
    </declare-styleable>

</resources>

4 사용자 정의 구성 요소 구현, ViewGroup의 하위 클래스 상속, 구성 방법 및 API 방법 구현
package com.andy.mobilesafe.ui;

import com.andy.mobilesafe.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * @author Zhang,Tianyou
 * @version 2014 11 15    10:22:50
 * 
 *                    TextView   checkbox   View
 */

public class SettingItemView extends RelativeLayout {

	private CheckBox cb_status;
	private TextView tv_title;
	private TextView tv_desc;

	private String desc_on;
	private String desc_off;

	/**
	 *        
	 * 
	 * @param context
	 */
	private void initView(Context context) {
		//          root            
		//         View     SettingItemView
		View.inflate(context, R.layout.setting_item_view, this);
		// View      SettingItemView
		cb_status = (CheckBox) this.findViewById(R.id.cb_status);
		tv_desc = (TextView) this.findViewById(R.id.tv_desc);
		tv_title = (TextView) this.findViewById(R.id.title);
	}

	public SettingItemView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		//               
		initView(context);
	}

	/**
	 *             ,           
	 * 
	 * @param context
	 * @param attrs
	 *                 
	 */
	public SettingItemView(Context context, AttributeSet attrs) {
		super(context, attrs);
		//                attrs              
		initView(context);

		String titles = attrs.getAttributeValue(
				"http://schemas.android.com/apk/res/com.andy.mobilesafe",
				"titles");
		desc_off = attrs.getAttributeValue(
				"http://schemas.android.com/apk/res/com.andy.mobilesafe",
				"desc_off");
		desc_on = attrs.getAttributeValue(
				"http://schemas.android.com/apk/res/com.andy.mobilesafe",
				"desc_on");
		tv_title.setText(titles);
		setDesc(desc_off);
	}

	public SettingItemView(Context context) {
		super(context);
		// new       
		initView(context);
	}

	/**
	 *            
	 * 
	 * @return
	 */
	public boolean isChecked() {
		return cb_status.isChecked();
	}

	/**
	 *         
	 * 
	 * @param checked
	 */
	public void setChecked(boolean checked) {
		if(checked){
			setDesc(desc_on);
		}else {
			setDesc(desc_off);
		}
		
		cb_status.setChecked(checked);
	}

	/**
	 *            
	 * 
	 * @param text
	 */
	public void setDesc(String text) {
		tv_desc.setText(text);
	}
}

좋은 웹페이지 즐겨찾기