最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android重寫View并自定義屬性實例分析

 更新時間:2016年02月17日 09:51:29   作者:xurong  
這篇文章主要介紹了Android重寫View并自定義屬性的方法,結合實例形式較為詳細的分析了Android基于重寫View實現自定義屬性的相關布局與具體技巧,需要的朋友可以參考下

本文實例分析了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>

第二步:在layout目錄下新建布局文件activity_main.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>

第三部繼承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);
    // 生成文字區(qū)域
    mBounds = new Rect();
  }
  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    // 獲取文字顯示區(qū)域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);
  }
}

運行!

更多關于Android相關內容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結

希望本文所述對大家Android程序設計有所幫助。

相關文章

  • Android 頁面多狀態(tài)布局管理的開發(fā)

    Android 頁面多狀態(tài)布局管理的開發(fā)

    頁面多狀態(tài)布局是開發(fā)中常見的需求,即頁面在不同狀態(tài)需要顯示不同的布局,這篇文章主要介紹了Android 頁面多狀態(tài)布局管理的開發(fā),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • Android退出應用最優(yōu)雅的方式(改進版)

    Android退出應用最優(yōu)雅的方式(改進版)

    這篇文章主要介紹了Android退出應用最優(yōu)雅的方式,改進版,感興趣的小伙伴們可以參考一下
    2016-01-01
  • Android EditText實現扁平化的登錄界面

    Android EditText實現扁平化的登錄界面

    這篇文章主要為大家詳細介紹了Android EditText實現扁平化的登錄界面,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android?Gson基本用法學習

    Android?Gson基本用法學習

    這篇文章介紹了Android?Gson的基本用法,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-12-12
  • Android自定義扇形倒計時實例代碼

    Android自定義扇形倒計時實例代碼

    最近工作中需要做一個倒計時,是那種一個圓,慢慢的被吃掉的動畫倒計時,由于自己是android小白,效果還不是多滿意,先給大家分享實例代碼,僅供大家參考
    2017-03-03
  • Android控件之TextView的分析探究

    Android控件之TextView的分析探究

    本篇文章介紹了,Android控件之TextView的分析探究。需要的朋友參考下
    2013-04-04
  • Android編程實現TextView部分顏色變動的方法

    Android編程實現TextView部分顏色變動的方法

    這篇文章主要介紹了Android編程實現TextView部分顏色變動的方法,涉及Android針對TextView樣式操作的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11
  • Android自定義view實現雪花特效實例代碼

    Android自定義view實現雪花特效實例代碼

    實現雪花的效果其實也可以通過自定義View的方式來實現的,而且操作上也相對簡單一些,下面這篇文章主要給大家介紹了關于Android自定義view實現雪花特效的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-12-12
  • Android根據電話號碼獲得聯系人頭像實例代碼

    Android根據電話號碼獲得聯系人頭像實例代碼

    這篇文章主要介紹了Android根據電話號碼獲得聯系人頭像實例代碼,是Android程序開發(fā)中非常重要的技巧,需要的朋友可以參考下
    2014-09-09
  • Android實現給TableLayou繪制邊框的方法

    Android實現給TableLayou繪制邊框的方法

    這篇文章主要介紹了Android實現給TableLayou繪制邊框的方法,涉及Android TableLayou布局控制相關技巧,需要的朋友可以參考下
    2016-03-03

最新評論

稷山县| 南京市| 安阳市| 项城市| 唐山市| 许昌市| 广灵县| 五指山市| 兴山县| 抚宁县| 鄂托克前旗| 西贡区| 北票市| 龙南县| 剑阁县| 吉隆县| 肇州县| 闵行区| 哈尔滨市| 罗甸县| 金堂县| 安庆市| 林周县| 延庆县| 桐梓县| 荥经县| 朝阳市| 织金县| 潜江市| 满洲里市| 赤水市| 青冈县| 云阳县| 滦南县| 司法| 霍山县| 利川市| 阿克苏市| 抚州市| 岑巩县| 关岭|