Android 재 작성 View 및 사용자 정의 속성 인 스 턴 스 분석

본 고 는 안 드 로 이 드 가 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);
  }
}

운행!
더 많은 안 드 로 이 드 관련 내용 에 관심 이 있 는 독자 들 은 본 사이트 의 주 제 를 볼 수 있다.
본 고 에서 말 한 것 이 여러분 의 안 드 로 이 드 프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다.

좋은 웹페이지 즐겨찾기