❶ android自定義布局的屬性獲取問題
下面圖片是android學習手冊中關於attr的實例圖,點擊源碼可以查看例子源碼,點擊文檔可以查看文檔。360手機助手中可下載。
java"><LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:test="http://schemas.android.com/apk/res/com.android.tutor"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<com.android.tutor.MyView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
test:textSize="20px"
test:textColor="#fff"
/>
</LinearLayout>
❷ 如何搭建android framework開發環境搭建
framework的開發比應用層就要煩的多啦。做應用在eclipse中就足夠了,用android系統中的控制項等工具,或者是自己寫個類來實現特定的功能。而framework層的開發,需要往源碼中添加代碼、xml、圖片、id等等數據,這個id可是費了我好大的勁才搞定的。在項目開始的一個半月里,我探索、嘗試了很多,現在把我的經驗分享出來。網上關於framework層的開發信息很少,多是靠自己。 最有效的方式就是分析android的源碼,看google是怎樣實現一個類的,以及類的層次。我現在看的主要是widget和app中的代碼,其他的還沒涉及。像View,ViewGroup,Activity,ActivityThread都是非常重要的類,也是代碼量很大的類,我只是大概地過了下,還沒有仔細分析過。 我花大力氣的地方是資源文件夾下values中幾個文件的作用。 attrs.xml中定義的是類的屬性,屬性是為了能在xml文件中被引用到,換句話說就是指定類中變數(也就是屬性的實際作用者)的值。這些屬性會在類的構造函數中用到。看過一兩個源碼就會明白,構造函數中的TypedArray其實就是屬性的數組,數組的成員會被賦給類里的成員,完成從xml的初始化。類的構造函數一般有三個,一個是Class(context),這個用於在代碼中創建一個類,所以只包含一個上下文;Class(context, attrs)和Class(context, attrs, defStyle)用於從xml創建類的情況。 styles.xml中是各個控制項的「樣式」,樣式由一個個屬性所組成。我們在編輯xml文件的時候,不同的類所能設置的屬性有共同的,也有不同的,都是由樣式來控制的。具體可以去看styles.xml這個文件的內容。 themes.xml其實也是樣式,只是適用的范圍大一點,這個我是參考網上別人的看法,自己還沒研究過。 ids.xml是公共的id,也就是對應用層可見的id,id是為了獲得xml中的對象而需要的參數,也就是Object = findViewById(R.id.id_name)中的id_name。這些值可以在代碼中用android.R.id引用到 public.xml描述的是為attr、id、drawable所指定的一個32的id值,這個值在current.xml文件中會被用到。 當我們對public.xml做了改動後,執行make update-api命令,相應的current.xml就會添加這些改動。因為這些值按類型被放在不同的段上,類型相同的必須連續存放,不指定id值的話系統會自動計算出下一個值。 添加id的方法:1)在ids.xml中添加公共id項,在public.xml中也添加相應的id項。這種辦法沒有兼容性,會與後面的版本產生沖突。 2)在ids.xml中添加公共id項,每一項前面加一行的注釋,這樣就不用在public.xml中添加相應的項了。源碼中是/** @hide */的形式,這樣current中就不會有相應的段了。 3)在自己寫的xml中,使用android:id = "@+id/id_name"的形式,就和在eclipse中一樣,搜一下會發現android源碼中也有這么用的,這樣ids.xml和public.xml都不用改,兼容性我還不確定。 上面這些希望能對大家有所幫助吧。
❸ android控制項參數 attribute怎麼寫
1.Attributeset自定義控制項和其他的控制項一樣,應該寫成一個類,而這個類的屬性是是有自...
2.首先要在res/values目錄下建立一個attrs.xml的文件,並在此文件中增加對控制項的屬性...
3.使用AttributeSet來完成控制項類的構造函數,並在構造函數中將自定義控制項類中變數
❹ Android自定義屬性TypedArray詳解
大家好,我是程序員雙木L,後續會發專題類的文章,這是自定義控制項的第一篇,之後也會陸續更新相關的文章,歡迎關注。
自定義屬性在自定義控制項過程中屬於比較常見的操作,我們可以回想一下這樣的場景:自定義view的過程中,我們需要在不同的情況下設置不同的文字大小,那麼我們是不是就需要提供對外的方法來設置,這樣就比較靈活操作。而我們自定義對外的方法,就是我們自定義的屬性啦,那我們來分析一下其原理及作用。
下面我們根據例子來進行分析:
1、首先我們需要在res->values目錄下新建attrs.xml文件,該文件就是用來聲明屬性名及其接受的數據格式的,如下:
attr名詞解析:
name表示屬性名,上面的屬性名是我自己定義的。
format表示接受的輸入格式,format格式集合如下:
2、自定義屬性的使用,這里我們使用兩種方式進行對比解析
最最最原始的使用方式
(1)、自定義文件如下:
我們可以在TestAttrsView方法的參數AttributeSet是個xml解析工具類,幫助我們從布局的xml里提取屬性名和屬性值。
(2)、在布局文件xml中的使用
這里使用自定義屬性需要聲明xml的命名空間,其中app是命名空間,用來加在自定義屬性前面。
xmlns:app=" http://schemas.android.com/apk/res-auto "
聲明xml命名空間,xmlns意思為「xml namespace」.冒號後面是給這個引用起的別名。
schemas是xml文檔的兩種約束文件其中的一種,規定了xml中有哪些元素(標簽)、
元素有哪些屬性及各元素的關系,當然從面向對象的角度理解schemas文件可以
認為它是被約束的xml文檔的「類」或稱為「模板」。
(3)、將屬性名與屬性值列印結果如下:
從列印結果我們可以看出,AttributeSet將布局文件xml下的屬性全部列印出來了,細心的童鞋可能已經看出來:
這個屬性我們設置的是一個整數尺寸,可最後列印出來的是資源編號。
那如果我們想要輸出我們設置的整數尺寸,需要怎麼操作呢?
這個時候就該我們這篇的主角出場了,使用TypedArray方式。
(1)、這里我們需要將attrs.xml使用「declare-styleable」標簽進行改造,如下:
從改造後的attrs文件可以看出,我們將屬性聲明歸結到TestStyleable裡面,也就意味著這些屬性是屬於TestStyleable下的。
(2)、屬性的解析:
這里我直接列印出解析結果,這里可以獲取我們想要的自定義屬性,而系統有的屬性可以忽略。
(3)、運行結果如下
從解析的結果可以看出,尺寸的結果已經轉換為實際值了:
這個時候有童鞋又問了,我設置的是15dp,為啥最後列印是41.25了呢?其實解析出來的值單位是px,所以這里輸出的是轉換後的值。
解析的過程中用到了這個方法:
我們來看一下這個方法的源碼:
源碼中我們可以看到這個方法有兩個參數:
obtainStyledAttributes方法返回值類型為TypedArray。該類型記錄了獲取到的屬性值集合,而通過數組下標索引即可找到對應的屬性值。索引下標通過R.styleable.TestStyleable_xx獲取,"xx"表示屬性名,一般命名為"styleable名" + "_" + "屬性名"。
而TypedArray提供了各種Api,如getInteger,getString,getDimension等方法來獲取屬性值,這些方法都需要傳入對應屬性名在obtainStyledAttributes中的int數組的位置索引,通過下標獲取數組里屬性值。
這個TypedArray的作用就是資源的映射作用,把自定義屬性在xml設置值映射到class,這樣怎麼獲取都很簡單啦。
到這里就分析完啦!
❺ android studio attrs.xml在哪兒
1. 定義一些自定義屬性
建立一個屬性xml文件: values/attrs.xml, 內容如下:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<!-- the relation between the icon and text. -->
<attr name="relation">
<enum name="icon_left" value="0" />
<enum name="icon_right" value="1" />
<enum name="icon_above" value="2" />
<enum name="icon_below" value="3" />
</attr>
<skip />
<declare-styleable name="IconText">
<attr name="relation" />
<attr name="icon" format="reference" />
<attr name="text" format="string" />
<attr name="text_size" format="dimension" />
<attr name="text_color" format="integer" />
<attr name="space" format="dimension" />
</declare-styleable>
❻ android中如何在xml文件中增加屬性
好的設置方式往往是數據驅動的,通過數據控制代碼的運行,便於代碼的維護和修改。在android中增中自定義的xml屬性, 可以把數據存儲在xml文件中,然後在代碼中取出這些屬性的值。分為以下幾步:
1. 在attrs.xml文件中定義屬性的類型,即字元串還是數值如下:
2.在xml 文件中指定譔屬性的值。如下:
android:key="tiltSensitivity"
android:defaultValue="50"
android:title="@string/preference_tilt_sensitivity"
android:summary="@string/preference_tilt_sensitivity_summary"
replica:maxText="@string/preference_tilt_max"
replica:minText="@string/preference_tilt_min"
android:persistent="true"
android:dependency="enableTiltControls"/>
3.在代碼中讀取值,方法如下:
public SliderPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.SliderPreference, defStyle, 0);
mMinText = a.getString(R.styleable.SliderPreference_minText);
❼ android中怎麼在View構造的attrs中拿到android給的屬性
//Android原生的屬性,都是提供方法可以獲得的,當然也可以通過
attrs獲得,而自定義的屬性獲得值方式如下,當然原生的也是一樣,只需要把attr name該成系統的。
一、 首先要在res/values目錄下建立一個attrs.xml(名字可以自己定義)的文件,並在此文件中增加對控制項的屬性的定義.其xml文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="my_text_view">
<attr name="text_size" format="float"></attr>
<attr name="text_color" format="color"></attr>
<attr name="text_back_ground" format="color|reference"></attr>
</declare-styleable>
</resources>
在這里,需要補充attrs屬性的相關知識,即Attr屬性是如何在XML中定義的,自定義屬性的Value值可以有10種類型以及其類型的組合值,其具體使用方法如下:
1. reference:參考某一資源ID。
(1)屬性定義:
<declare-styleable name = "名稱">
<attr name = "background" format = "reference" />
</declare-styleable>
(2)屬性使用:
<ImageView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/圖片ID"
/>
2. color:顏色值。
(1)屬性定義:
<declare-styleable name = "名稱">
<attr name = "textColor" format = "color" />
</declare-styleable>
(2)屬性使用:
<TextView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:textColor = "#00FF00"
/>
3. boolean:布爾值。
(1)屬性定義:
<declare-styleable name = "名稱">
<attr name = "focusable" format = "boolean" />
</declare-styleable>
(2)屬性使用:
<Button
android:layout_width = "42dip"
android:layout_height = "42dip"
android:focusable = "true"
/>
4. dimension:尺寸值。
(1)屬性定義:
<declare-styleable name = "名稱">
<attr name = "layout_width" format = "dimension" />
</declare-styleable>
(2)屬性使用:
<Button
android:layout_width = "42dip"
android:layout_height = "42dip"
/>
5. float:浮點值。
(1)屬性定義:
<declare-styleable name = "AlphaAnimation">
<attr name = "fromAlpha" format = "float" />
<attr name = "toAlpha" format = "float" />
</declare-styleable>
(2)屬性使用:
<alpha
android:fromAlpha = "1.0"
android:toAlpha = "0.7"
/>
6. integer:整型值。
(1)屬性定義:
<declare-styleable name = "AnimatedRotateDrawable">
<attr name = "visible" />
<attr name = "frameDuration" format="integer" />
<attr name = "framesCount" format="integer" />
<attr name = "pivotX" />
<attr name = "pivotY" />
<attr name = "drawable" />
</declare-styleable>
(2)屬性使用:
<animated-rotate
xmlns:android = "http://schemas.android.com/apk/res/android"
android:drawable = "@drawable/圖片ID"
android:pivotX = "50%"
android:pivotY = "50%"
android:framesCount = "12"
android:frameDuration = "100"
/>
7. string:字元串。
(1)屬性定義:
<declare-styleable name = "MapView">
<attr name = "apiKey" format = "string" />
</declare-styleable>
(2)屬性使用:
<com.google.android.maps.MapView
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:apiKey = "_bc_g"
/>
8. fraction:百分數。
(1)屬性定義:
<declare-styleable name="RotateDrawable">
<attr name = "visible" />
<attr name = "fromDegrees" format = "float" />
<attr name = "toDegrees" format = "float" />
<attr name = "pivotX" format = "fraction" />
<attr name = "pivotY" format = "fraction" />
<attr name = "drawable" />
</declare-styleable>
(2)屬性使用:
<rotate
xmlns:android = "http://schemas.android.com/apk/res/android"
android:interpolator = "@anim/動畫ID"
android:fromDegrees = "0"
android:toDegrees = "360"
android:pivotX = "200%"
android:pivotY = "300%"
android:ration = "5000"
android:repeatMode = "restart"
android:repeatCount = "infinite"
/>
9. enum:枚舉值。
(1)屬性定義:
<declare-styleable name="名稱">
<attr name="orientation">
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr>
</declare-styleable>
(2)屬性使用:
<LinearLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:orientation = "vertical"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
>
</LinearLayout>
10. flag:位或運算。
(1)屬性定義:
<declare-styleable name="名稱">
<attr name="windowSoftInputMode">
<flag name = "stateUnspecified" value = "0" />
<flag name = "stateUnchanged" value = "1" />
<flag name = "stateHidden" value = "2" />
<flag name = "stateAlwaysHidden" value = "3" />
<flag name = "stateVisible" value = "4" />
<flag name = "stateAlwaysVisible" value = "5" />
<flag name = "adjustUnspecified" value = "0x00" />
<flag name = "adjustResize" value = "0x10" />
<flag name = "adjustPan" value = "0x20" />
<flag name = "adjustNothing" value = "0x30" />
</attr>
</declare-styleable>
(2)屬性使用:
<activity
android:name = ".StyleAndThemeActivity"
android:label = "@string/app_name"
android:windowSoftInputMode = "stateUnspecified | stateUnchanged|stateHidden">
<intent-filter>
<action android:name = "android.intent.action.MAIN" />
<category android:name = "android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
注意:
屬性定義時可以指定多種類型值。
(1)屬性定義:
<declare-styleable name = "名稱">
<attr name = "background" format = "reference|color" />
</declare-styleable>
(2)屬性使用:
<ImageView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/圖片ID|#00FF00"
/>
二、接下來實現自定義View的類,其中下面的構造方法是重點,在代碼中獲取自定義屬性,其代碼如下:
package com.example.CustomAttr;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.TextView;
/**
* Created with IntelliJ IDEA.
* User: wen.nan
* Date: 13-9-28
* Time: 下午10:00
* To change this template use File | Settings | File Templates.
*/
public class CustomTextView extends TextView {
private TypedArray mTypedArray;
private Paint mPaint;
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
inial(context,attrs);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
inial(context,attrs);
}
private void inial(Context context,AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.my_text_view);
float textsize = typedArray.getFloat(R.styleable.my_text_view_text_size,14) ;
int textColor = typedArray.getColor(R.styleable.my_text_view_text_color,0xFFFFFF) ;
int bgColor = typedArray.getColor(R.styleable.my_text_view_text_back_ground,0xFFFFFF) ;
super.setTextColor(textColor);
super.setTextSize(textsize);
super.setBackgroundColor(bgColor);
typedArray.recycle();
}
}
三、接下來在XML布局中引用自定義View控制項,其XML代碼如下:
?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app = "http://schemas.android.com/apk/res/com.example.CustomAttr"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.example.CustomAttr.CustomTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, MainActivity"
app:text_size ="20"
app:text_color ="#00FF00"
app:text_back_ground="#ffffff"
/>
</LinearLayout>
注意上面XML中代碼: xmlns:app = "http://schemas.android.com/apk/res/com.example.CustomAttr",是自定義的app命名空間,res後面是應用程序包名,然後可以直接使用app:text_size,等屬性,其值類型要和attrs.xml定義的屬性Value值相對應。
四、總結:
注意該例子中是使用app:text_size = "20 和app:text_color="#00FF00定義TextView的顏色和textView的字體大小,而不是使用系統的屬性android:textsize等。該例子中只是起到拋磚引玉的作用,你可以自定義其他屬性,來實現你想要的自定義View效果。