Android에서 개발한 사용자 정의 UI 구성 요소 및 속성
6725 단어 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(속성 파일 만들기)에서 속성을 정의합니다.
<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);
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Application Development in WebOSDevelop applications in WebOS Recognize the abstract class Application The operating system accepts classes that impleme...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.