Android 재 작성 View 및 사용자 정의 속성 인 스 턴 스 분석
다음 그림 과 같은 사용자 정의 속성 을 사용 합 니 다.
첫 번 째 단계:res\values 디 렉 터 리 에 새 파일 attrs.xml
사용자 정의 속성 설명
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomViewStyle">
<attr name="customText" format="string" />
<attr name="customTextColor" format="color" />
<attr name="customTextSize" format="dimension" />
</declare-styleable>
</resources>
두 번 째 단계:layot 디 렉 터 리 에 새 레이아웃 파일 activitymain.xml특히 외부 컨트롤 에 이 성명 을 추가 해 야 합 니 다.
형식:xmlns:(사용자 정의 이름)="http://schemas.android.com/apk/(당신 이 적용 한 가방 이름)"
xmlns:xr="http://schemas.android.com/apk/res/com.rong.test"
혹은
xmlns:xr="http://schemas.android.com/apk/res-auto"
추천레이아웃 파일 에 사용자 정의 속성 을 추가 합 니 다:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:xr="http://schemas.android.com/apk/res/com.rong.test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:orientation="vertical" >
<com.rong.activity.CustomView
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerInParent="true"
android:background="#ff0000"
xr:customText=" "
xr:customTextColor="#000000"
xr:customTextSize="40sp" />
</RelativeLayout>
3 부 상속 View 재 작성
package com.rong.activity;
import com.rong.test.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
/**
*
*
* @author
*
*/
public class CustomView extends View {
/**
*
*/
private Paint mPaint;
/**
*
*/
private Rect mBounds;
/**
*
*/
private String customText;
/**
*
*/
private int customTextSize;
/**
*
*/
private int customTextColor;
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomViewStyle);
//
customText = typedArray.getString(R.styleable.CustomViewStyle_customText);
//
customTextSize = typedArray.getDimensionPixelSize(R.styleable.CustomViewStyle_customTextSize, 28);
//
customTextColor = typedArray.getColor(R.styleable.CustomViewStyle_customTextColor, Color.WHITE);
// typedArray
typedArray.recycle();
initView();
}
public void initView() {
//
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(customTextColor);
mPaint.setTextSize(customTextSize);
//
mBounds = new Rect();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// mBounds
mPaint.getTextBounds(customText, 0, customText.length(), mBounds);
// = /2 - /2
float helfWidth = getWidth() / 2 - mBounds.width() / 2;
// = /2 + /2
float helfHeight = getHeight() / 2+mBounds.height()/2;
//
canvas.drawText(customText, helfWidth, helfHeight, mPaint);
}
}
운행!더 많은 안 드 로 이 드 관련 내용 에 관심 이 있 는 독자 들 은 본 사이트 의 주 제 를 볼 수 있다.
본 고 에서 말 한 것 이 여러분 의 안 드 로 이 드 프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.