日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

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

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

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

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/smile1"

iconSrc="@drawable/smile"/>

(2)在構(gòu)造函數(shù)中拿到這個值

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,其中第一個參數(shù)為命名空間。

//命名空間

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來實現(xiàn)

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

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

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的相關(guān)技術(shù),現(xiàn)在就android中解析文件的三種方式做以下總結(jié).其主要有:SAX(Simple API fo ...

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

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

Struts中的數(shù)據(jù)處理的三種方式

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

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

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

JS中事件綁定的三種方式

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

一道經(jīng)典的C++結(jié)構(gòu)體的題目

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

CrashMe分析教程1 - BreakPoint

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

PHP7類型約束

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

java關(guān)鍵字保留字

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

pyspider和pyquery總結(jié)

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

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。