日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

android获取自定义属性,android 自定义控件中获取属性的三种方式(转)

發布時間:2025/3/20 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android获取自定义属性,android 自定义控件中获取属性的三种方式(转) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

第一種方法,直接設置屬性值,通過attrs.getAttributeResourceValue拿到這個屬性值。

(1)在xml文件中設置屬性值

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/smile1"

iconSrc="@drawable/smile"/>

(2)在構造函數中拿到這個值

public IconTextView(Context context, AttributeSet attrs) {

super(context, attrs);

resourceID = attrs.getAttributeResourceValue(null, "iconSrc", 0);

if(resourceID > 0) {

bitmap = BitmapFactory.decodeResource(getResources(), resourceID);

}

}

第二種方法,使用自己的命名空間

(1)注意在xml文件中,需要聲明一個命名空間,形式為http:// + 這個VIEW的包名

xmlns:mobile="http://com.example.activity"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/smile1"

mobile:iconSrc="@drawable/smile"/>

(2)通過attrs.getAttributeResourceValue,其中第一個參數為命名空間。

//命名空間

private?final?String?namespace?=?"http://com.example.activity"

public IconTextView(Context context, AttributeSet attrs) {

super(context, attrs);

resourceID = attrs.getAttributeResourceValue(namespace, "iconSrc", 0);

// TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.IconTextView);

// resourceID = array.getResourceId(R.styleable.IconTextView_iconSrc, 0);

if(resourceID > 0) {

bitmap = BitmapFactory.decodeResource(getResources(), resourceID);

}

}

第三種方法,通過自定義attrs.xml來實現

(1)自定義一個attrs.xml文件

(2)在xml文件中使用這一屬性,注意此時命名空間的書寫規范。

xmlns:mobile="http://schemas.android.com/apk/res/com.example.activity"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/smile1"

mobile:iconSrc="@drawable/smile"/>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/smile2"

android:textSize="24dp"

mobile:iconSrc="@drawable/smile"/>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/smile3"

android:textSize="36dp"

mobile:iconSrc="@drawable/smile"/>

(3)在代碼中使用context.obtainStyledAttributes獲得屬性值

package com.example.activity;

import android.content.Context;

import android.content.res.TypedArray;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Canvas;

import android.graphics.Rect;

import android.util.AttributeSet;

import android.widget.TextView;

public class IconTextView extends TextView {

//命名空間

private final String namespace = "http://com.example.activity";

//資源ID

private int resourceID = 0;

private Bitmap bitmap;

public IconTextView(Context context, AttributeSet attrs) {

super(context, attrs);

TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.IconTextView);

resourceID = array.getResourceId(R.styleable.IconTextView_iconSrc, 0);

if(resourceID > 0) {

bitmap = BitmapFactory.decodeResource(getResources(), resourceID);

}

}

@Override

public void onDraw(Canvas canvas) {

if (bitmap != null) {

Rect src = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

Rect target = new Rect();

int textHeight = (int)getTextSize();

target.left = 0;

target.top =(int)(getMeasuredHeight() - getTextSize()) / 2 + 1;

target.bottom = target.top + textHeight;

target.right = (int)(textHeight * (bitmap.getWidth() / (float)bitmap.getHeight()));

canvas.drawBitmap(bitmap, src, target, getPaint());

canvas.translate(target.right + 2, 0);

}

super.onDraw(canvas);

}

}

【Struts2】Struts2獲取session的三種方式

1.Map map =? ActionContext.getContext().getSession(); 2.HttpSession session = S ...

android中解析文件的三種方式

android中解析文件的三種方式 ? ? 好久沒有動手寫點東西了,最近在研究android的相關技術,現在就android中解析文件的三種方式做以下總結.其主要有:SAX(Simple API fo ...

Struts2(四.注冊時檢查用戶名是否存在及Action獲取數據的三種方式)

一.功能 1.用戶注冊頁面

Struts中的數據處理的三種方式

Struts中的數據處理的三種方式: public class DataAction extends ActionSupport{ @Override public String execute() ...

OpenCV4Android釋疑: 透析Android以JNI調OpenCV的三種方式(讓OpenCVManager永不困擾)

OpenCV4Android釋疑: 透析Android以JNI調OpenCV的三種方式(讓OpenCVManager永不困擾) 前文曾詳細探討了關于OpenCV的使用,原本以為天下已太平.但不斷有人反 ...

JS中事件綁定的三種方式

以下是搜集的在JS中事件綁定的三種方式. ? 1. HTML onclick attribute ? ?

一道經典的C++結構體的題目

題目描述: 有10個學生,每個學生的數據包括學號.姓名.英語.數學.物理三門課的成績,從鍵盤輸入10個學生數據,要求打印出3門課程的總平均成績,以及最高分的學生的數據(包括學號,姓名,3門課的平均成績 ...

CrashMe分析教程1 - BreakPoint

首先,謝謝?Robert Kuster?為我們提供了這么好的CrashMe項目.?很多人想尋找一個CrashMe分析的教程, 我也想要, 但是似乎網絡里沒有, 所以我就決定用業余時間寫一個小系列來共享 ...

PHP7類型約束

在PHP7之前,函數和類方法不需要聲明變量類型,任何數據都可以被傳遞和返回,導致幾乎大部分的調用操作都要判斷返回的數據類型是否合格. 為了解決這個問題,PHP7引入了類型聲明. 目前有兩類變量可以聲明 ...

java關鍵字保留字

Here is a list of keywords in the Java programming language. You cannot use any of the following as ...

pyspider和pyquery總結

1.參考 pyspider作者官網: pyspider 爬蟲教程(一):HTML 和 CSS 選擇器 pyspider 爬蟲教程(二):AJAX 和 HTTP pyspider 爬蟲教程(三):使用 ...

總結

以上是生活随笔為你收集整理的android获取自定义属性,android 自定义控件中获取属性的三种方式(转)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。