導航:首頁 > 操作系統 > androidattr的使用

androidattr的使用

發布時間:2023-01-09 17:53:57

A. android 資源文件 string 和attr的區別

相比attr,prop是1.6.1才新出來的,兩者從中文意思理解,都是獲取/設置屬性的方法(attributes和properties)。只是,window或document中使用.attr()方法在jQuery1.6之前不能正常運行,因為window和document中不能有attributes。prop應運而生了。
attr:
描述: 獲取匹配的元素集合中的第一個元素的屬性的值。
attr( attributeName )
attributeName
類型: String
要獲取的屬性名稱 .attr()方法只獲取第一個匹配元素的屬性值。

prop:

描述: 獲取匹配的元素集中第一個元素的屬性(property)值
prop( propertyName )
propertyName
類型: String
要得到的屬性的名稱 .prop()方法只獲得第一個匹配元素的屬性值 。如果元素上沒有該屬性,或者如果沒有匹配的元素。那麼該方法會返回undefined值。

B. Android屬性匯總

TextView屬性匯總

android:autoLink設置是否當文本為URL鏈接/email/電話號碼/map時,文本顯示為可點擊的鏈接。可選值(none/web/email/phone/map/all)

android:autoText如果設置,將自動執行輸入值的拼寫糾正。此處無效果,在顯示輸入法並輸入的時候起作用。

android:bufferType指定getText()方式取得的文本類別。選項editable 類似於StringBuilder可追加字元,也就是說getText後可調用append方法設置文本內容。spannable 則可在給定的字元區域使用樣式,參見這里1、這里2。

android:capitalize設置英文字母大寫類型。此處無效果,需要彈出輸入法才能看得到,參見EditView此屬性說明。

android:cursorVisible設定游標為顯示/隱藏,默認顯示。

android:digits設置允許輸入哪些字元。如「1234567890.+-*/% ()」

android:drawableBottom在text的下方輸出一個drawable,如圖片。如果指定一個顏色的話會把text的背景設為該顏色,並且同時和background使用時覆蓋後者。

android:drawableLeft在text的左邊輸出一個drawable,如圖片。

android:drawablePadding設置text與drawable(圖片)的間隔,與drawableLeft、 drawableRight、drawableTop、drawableBottom一起使用,可設置為負數,單獨使用沒有效果。

android:drawableRight在text的右邊輸出一個drawable。

android:drawableTop在text的正上方輸出一個drawable。

android:editable設置是否可編輯。

android:editorExtras設置文本的額外的輸入數據。

android:ellipsize設置當文字過長時,該控制項該如何顯示。有如下值設置:」start」—-省略號顯示在開頭;」end」 ——省略號顯示在結尾;」middle」—-省略號顯示在中間;」marquee」 ——以跑馬燈的方式顯示(動畫橫向移動)

android:freezesText設置保存文本的內容以及游標的位置。

android:gravity設置文本位置,如設置成「center」,文本將居中顯示。

android:hintText為空時顯示的文字提示信息,可通過textColorHint設置提示信息的顏色。此屬性在 EditView中使用,但是這里也可以用。

android:imeOptions附加功能,設置右下角IME動作與編輯框相關的動作,如actionDone右下角將顯示一個「完成」,而不設置默認是一個回車符號。這個在EditView中再詳細說明,此處無用。

android:imeActionId設置IME動作ID。

android:imeActionLabel設置IME動作標簽。

android:includeFontPadding設置文本是否包含頂部和底部額外空白,默認為true。

android:inputMethod為文本指定輸入法,需要完全限定名(完整的包名)。例如:com.google.android.inputmethod.pinyin,但是這里報錯找不到。

android:inputType設置文本的類型,用於幫助輸入法顯示合適的鍵盤類型。在EditView中再詳細說明,這里無效果。

android:linksClickable設置鏈接是否點擊連接,即使設置了autoLink。

android:marqueeRepeatLimit在ellipsize指定marquee的情況下,設置重復滾動的次數,當設置為 marquee_forever時表示無限次。

android:ems設置TextView的寬度為N個字元的寬度。這里測試為一個漢字字元寬度

android:maxEms設置TextView的寬度為最長為N個字元的寬度。與ems同時使用時覆蓋ems選項。

android:minEms設置TextView的寬度為最短為N個字元的寬度。與ems同時使用時覆蓋ems選項。

android:maxLength限制顯示的文本長度,超出部分不顯示。

android:lines設置文本的行數,設置兩行就顯示兩行,即使第二行沒有數據。

android:maxLines設置文本的最大顯示行數,與width或者layout_width結合使用,超出部分自動換行,超出行數將不顯示。

android:minLines設置文本的最小行數,與lines類似。

android:lineSpacingExtra設置行間距。

android:lineSpacingMultiplier設置行間距的倍數。如」1.2」

android:numeric如果被設置,該TextView有一個數字輸入法。此處無用,設置後唯一效果是TextView有點擊效果,此屬性在EdtiView將詳細說明。

android:password以小點」.」顯示文本

android:phoneNumber設置為電話號碼的輸入方式。

android:privateImeOptions設置輸入法選項,此處無用,在EditText將進一步討論。

android:scrollHorizontally設置文本超出TextView的寬度的情況下,是否出現橫拉條。

android:selectAllOnFocus如果文本是可選擇的,讓他獲取焦點而不是將游標移動為文本的開始位置或者末尾位置。 TextView中設置後無效果。

android:shadowColor指定文本陰影的顏色,需要與shadowRadius一起使用。

android:shadowDx設置陰影橫向坐標開始位置。

android:shadowDy設置陰影縱向坐標開始位置。

android:shadowRadius設置陰影的半徑。設置為0.1就變成字體的顏色了,一般設置為3.0的效果比較好。

android:singleLine設置單行顯示。如果和layout_width一起使用,當文本不能全部顯示時,後面用「…」來表示。如android:text="test_ singleLine "

android:singleLine="true" android:layout_width="20dp"將只顯示「t…」。如果不設置singleLine或者設置為false,文本將自動換行

android:text設置顯示文本.

android:textAppearance設置文字外。如 「?android:attr/textAppearanceLargeInverse」這里引用的是系統自帶的一個外觀,?表示系統是否有這種外觀,否則使用默認的外觀。可textAppearanceButton/textAppearanceInverse/textAppearanceLarge/textAppearanceLargeInverse/textAppearanceMedium/textAppearanceMediumInverse/textAppearanceSmall/textAppearanceSmallInverse

android:textColor設置文本顏色

android:textColorHighlight被選中文字的底色,默認為藍色

android:textColorHint設置提示信息文字的顏色,默認為灰色。與hint一起使用。

android:textColorLink文字鏈接的顏色.

android:textScaleX設置文字之間間隔,默認為1.0f。

android:textSize設置文字大小,推薦度量單位」sp」,如」15sp」

android:textStyle設置字形[bold(粗體) 0, italic(斜體) 1, bolditalic(又粗又斜) 2] 可以設置一個或多個,用「|」隔開

android:typeface設置文本字體,必須是以下常量值之一:normal 0, sans 1, serif 2, monospace(等寬字體) 3]

android:height設置文本區域的高度,支持度量單位:px(像素)/dp/sp/in/mm(毫米)

android:maxHeight設置文本區域的最大高度

android:minHeight設置文本區域的最小高度

android:width設置文本區域的寬度,支持度量單位:px(像素)/dp/sp/in/mm(毫米),與layout_width 的區別看這里。

android:maxWidth設置文本區域的最大寬度

android:minWidth設置文本區域的最小寬度

Android activity屬性匯總

android:allowTaskReparenting

是否允許activity更換從屬的任務,比如從簡訊息任務 切換到瀏覽器任務。

android:alwaysRetainTaskState

是否保留狀態不變, 比如切換回home, 再從新打開, activity處於最後的狀態

android:clearTaskOnLanunch

比如 P 是 activity, Q 是被P 觸發的 activity, 然後返回Home, 從新啟動 P, 是否顯示 Q

android:configChanges

當配置list發生修改時,是否調用 onConfigurationChanged() 方法 比如 "locale|navigation|orientation".

android:enabled

activity 是否可以被實例化,

android:excludeFromRecents

是否可被顯示在最近打開的activity列表裡

android:exported

是否允許activity被其它程序調用

android:finishOnTaskLaunch

是否關閉已打開的activity當用戶重新啟動這個任務的時候

android.icon

android:label

android:launchMode

activity啟動方式, "standard" "singleTop" "singleTask" "singleInstance"

其中前兩個為一組, 後兩個為一組

android:multiprocess

允許多進程

android:name

activity的類名, 必須指定

androidnHistory

是否需要移除這個activity當用戶切換到其他屏幕時。 這個屬性是 API level 3 中引入的

android:permission

android:process

一 個activity運行時所在的進程名,所有程序組件運行在應用程序默認的進程中,這個進程名跟應用程序的包名一致。中的元素process屬性能夠為所有組件設定一個新的默認值。但是任何組件都可以覆蓋這個默認值,允許你將你的程序放在多進程中運行。 如果這個屬性被分配的名字以:開頭,當這個activity運行時, 一個新的專屬於這個程序的進程將會被創建。 如果這個進程名以小寫字母開頭,這個activity將會運行在全局的進程中,被它的許可所提供。

android:screenOrientation

activity顯示的模式, "unspecified" 默認值 "landscape" 風景畫模式,寬度比高度大一些 "portrait" 肖像模式, 高度比寬度大。 "user" 用戶的設置 "behind" "sensor" "nosensor"

android:stateNotNeeded

是否 activity被銷毀和成功重啟並不保存狀態

android:taskAffinity

activity的親屬關系, 默認情況同一個應用程序下的activity有相同的關系

android:theme

activity的樣式主題, 如果沒有設置,則activity的主題樣式從屬於應用程序, 參見元素的theme屬性

android:windowSoftInputMode

activity主窗口與軟鍵盤的交互模式, 自從API level 3 被引入

活動的主窗口如何與包含屏幕上的軟鍵盤窗口交互。這個屬性的設置將會影響兩件事情:

1>    軟鍵盤的狀態——是否它是隱藏或顯示——當活動(Activity)成為用戶關注的焦點。

2>    活動的主窗口調整——是否減少活動主窗口大小以便騰出空間放軟鍵盤或是否當活動窗口的部分被軟鍵盤覆蓋時它的內容的當前焦點是可見的。

它的設置必須是下面列表中的一個值,或一個」state…」值加一個」adjust…」值的組合。在任一組設置多個值——多個」state…」values,例如&mdash有未定義的結果。各個值之間用|分開。例如: <activity android:windowSoftInputMode="stateVisible|adjustResize" . . . >

在這設置的值(除"stateUnspecified"和"adjustUnspecified"以外)將覆蓋在主題中設置的值

值 描述

"stateUnspecified" 軟鍵盤的狀態(是否它是隱藏或可見)沒有被指定。系統將選擇一個合適的狀態或依賴於主題的設置。這個是為了軟體盤行為默認的設置。

"stateUnchanged" 軟鍵盤被保持無論它上次是什麼狀態,是否可見或隱藏,當主窗口出現在前面時。

"stateHidden" 當用戶選擇該Activity時,軟鍵盤被隱藏——也就是,當用戶確定導航到該Activity時,而不是返回到它由於離開另一個Activity。

"stateAlwaysHidden" 軟鍵盤總是被隱藏的,當該Activity主窗口獲取焦點時。

"stateVisible" 軟鍵盤是可見的,當那個是正常合適的時(當用戶導航到Activity主窗口時)。

"stateAlwaysVisible" 當用戶選擇這個Activity時,軟鍵盤是可見的——也就是,也就是,當用戶確定導航到該Activity時,而不是返回到它由於離開另一個Activity。

"adjustUnspecified" 它不被指定是否該Activity主窗口調整大小以便留出軟鍵盤的空間,或是否窗口上的內容得到屏幕上當前的焦點是可見的。系統將自動選擇這些模式中一種主要依賴於是否窗口的內容有任何布局視圖能夠滾動他們的內容。如果有這樣的一個視圖,這個窗口將調整大小,這樣的假設可以使滾動窗口的內容在一個較小的區域中可見的。這個是主窗口默認的行為設置。

"adjustResize" 該Activity主窗口總是被調整屏幕的大小以便留出軟鍵盤的空間。

"adjustPan" 該Activity主窗口並不調整屏幕的大小以便留出軟鍵盤的空間。相反,當前窗口的內容將自動移動以便當前焦點從不被鍵盤覆蓋和用戶能總是看到輸入內容的部分。這個通常是不期望比調整大小,因為用戶可能關閉軟鍵盤以便獲得與被覆蓋內容的交互操作。

Android EditText 屬性匯總

android:layout_gravity="center_vertical"

設置控制項顯示的位置:默認top,這里居中顯示,還有bottom

android:hint="請輸入數字!"

設置顯示在空間上的提示信息

android:numeric="integer"

設置只能輸入整數,如果是小數則是:decimal

android:singleLine="true"

設置單行輸入,一旦設置為true,則文字不會自動換行。

android:password="true"

設置只能輸入密碼

android:textColor = "#ff8c00"

字體顏色

android:textStyle="bold"

字體,bold, italic, bolditalic

android:textSize="20dip"

大小

android:capitalize = "characters"

以大寫字母寫

android:textAlign="center"

EditText沒有這個屬性,但TextView有

android:textColorHighlight="#cccccc"

被選中文字的底色,默認為藍色

android:textColorHint="#ffff00"

設置提示信息文字的顏色,默認為灰色

android:textScaleX="1.5"

控制字與字之間的間距

android:typeface="monospace"

字型,normal, sans, serif, monospace

android:background="@null"

空間背景,這里沒有,指透明

android:layout_weight="1"

權重,控制控制項之間的地位,在控制控制項顯示的大小時蠻有用的。

android:textAppearance="?android:attr/textAppearanceLargeInverse"

文字外觀,這里引用的是系統自帶的一個外觀,?表示系統是否有這種外觀,否則使用默認的外觀。不知道這樣理解對不對?

通過EditText的layout xml文件中的相關屬性來實現:

  1. 密碼框屬性 android:password="true"  這條可以讓EditText顯示的內容自動為星號,輸入時內容會在1秒內變成*字樣。

  2. 純數字 android:numeric="true" 這條可以讓輸入法自動變為數字輸入鍵盤,同時僅允許0-9的數字輸入

  3. 僅允許 android:capitalize="cwj1987" 這樣僅允許接受輸入cwj1987,一般用於密碼驗證

  下面是一些擴展的風格屬性

  android:editable="false" 設置EditText不可編輯

  android:singleLine="true" 強制輸入的內容在單行

  android:ellipsize="end" 自動隱藏尾部溢出數據,一般用於文字內容過長一行無法全部顯示時

---------------------

C. 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效果。

D. Android基礎知識

一、activity

1.一個activity就是一個類,繼承activity;

2.需要復寫onCreate()方法;

3.每一個activity都需要在AndroidMainfest.xml清單上進行配置;

4.為activity添加必要的控制項。

二、布局

線性布局:LinearLayout

1.填滿父空間:fill_parent、match_parent

2.文本多大空間就有多大:warp_content

3.文字對齊方式:gravity

4.占屏幕的比例:layout_weight="1"  水平方向,則width=0,垂直方向,則height=0

5.一行顯示,空間不夠會省略:singleLine="ture"  false會換行

6.背景:background="#ffffff"

7.水平布局:orientation="horizontal"

垂直布局:orientation="vertivcal"

表格布局:TableLayout

1.內邊距:padding

2.外邊距:marginLeft\Start、Right\End、Top、Bottom

三、RelativeLayout相對布局

layout_above 將該控制項的底部置於給定ID控制項之上

layout_below 將該控制項的頂部置於給定ID控制項之下

layout_toLeftOf 將該控制項的右邊緣和給定ID控制項的左邊緣對齊

layout_toRightOf 將該控制項的左邊緣和給定ID控制項的右邊緣對齊

layout_alignBaseline 該控制項的baseline和給定ID的控制項的Baseline對齊

layout_alignBottom 該控制項的底部邊緣和給定ID的控制項的底部邊緣對齊

layout_alignLeft 該控制項的左邊緣和給定ID的控制項的左邊緣對齊

layout_alignRight 該控制項的右邊緣和給定ID的控制項的右邊緣對齊

layout_alignTop 該控制項的頂部邊緣和給定ID的控制項的頂部邊緣對齊

layout_alignparentBottom 如果該值為true,則該控制項的底部和父控制項的底部對齊layout_alignParentLeft 如果該值為true,則該控制項的左邊和父控制項的左邊對齊

layout_alignParentRight 如果該值為true,則該控制項的右邊和父控制項的右邊對齊

layout_alignParentTop 如果該值為true,則該控制項的上邊和父控制項的上邊對齊

layout_centerHorizontal 如果該值為true,則該控制項將被置於水平方向的中央

layout_centerInParent 如果該值為true,則該控制項將被置於父控制項水平和垂直方向的中央

layout_centerVertival 如果該值為true,則該控制項將被置於垂直方向的中央

四、一個Intent對象包含一組信息

1.Component name

2.Action

3.Data

4.Category

5.Extras

6.Flags

Intent intent = new Intent(this, SecondActivity.class);

startActivity(intent);  //startActivity方法

intent.putExtra("Key", "Value");  //鍵值對

intent = getIntent();

String value = intent.getStringExtra("Key");    //通過鍵提取數據

五、初級控制項:EditText、TextView、Button

1.獲取EditText的值

String value = EditText.getText().toString();

2.將值放到Intent對象中

Intent intent = new Intent();

intent.putExtra("one",value )

intent.setCalss(Activity.this, OtherActivity.class);

3.使用這個Intent對象來啟動Otheractivity

Activity.this.startActivity(intent);

4.將監聽器的對象綁定到按鈕對象上

button.setOnclickListener(new Listener());

5.得到Intent對象當中的值

Intent intent = getIntent();

String value1 = intent.getStringExtra("one");

int value2 = Integer.parseInt(value);

六、其他初級控制項使用

①ImageView

②RadioGroup和RadioButton

setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener())

③Checkbox

setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener())

④Menu

1.當客戶點擊MENU按鈕的時候,調用onCreateOptionMenu()方法

public boolean onCreateOptionMenu(Menu menu){

menu.add(0,1,1,R.string.id);

}

2.當客戶點擊MENU內部的具體某一個選項時,調用onOptionItemSelected()方法

public boolean onOptionItemSelected(MenuItem item){

if(item.getItemId() == 1){

finish();

}

return super.onOptionItemSelected(item);

}

七、Activity的生命周期

1.第一次創建時調用

protected void onCreat(Bundle saveInstanceState);

2.顯示出來時調用

protected void onStrat();

3.獲得用戶焦點時調用(可操作)

protected void onResume();

4.點擊彈出第二個Activity時調用

protected void onPause();

5.當第一個Activity不可見時調用

protected void onStop();

6.當返回第一個Activity時調用,代替OnCreate,因為沒被銷毀

protected void onRestart();

7.當返回第一個Activity時調用(先執行onStop,在執行,因為第二個Activity被銷毀,不能返回獲取,只能通過onCreat,onStart,onResume再創建)

protected void onDestory();

八、Task

1.Task是存放Activity的Stack棧。當點擊啟動第二個Activiry時,第一個Activtiy會被壓入Stack棧當中,第二個Activity會位於棧的頂部;當返回第一個Activtiy時,第二個Activity會被彈出Stack,第一個Activity會位於棧的頂部,以此類推。

注釋:當調用finish()時,當前的Activity會被Destory掉,棧中的Activity會消失。

2.當Activity都從Stack退出後,則就不存在Task。

九、高級控制項

①進度條ProgressBar

水平進度條style="?android:attr/progressBarStyleHorizontal"

圓圈進度條style="?android:attr/progressBarStyle"

用戶可視的visibility="gone"

②列表ListView

十、其他控制項

A.下拉菜單Spinner

1.創建一個ArrayAdapter:

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(

this, //指上下文對象

R.array.plant_array, //引用了在文件中定義的String數組

android.R.layout.simple_spinner_item);//用來指定Spinner的樣式,可替換自定義

adapter.setDropDownViewResource(

android.R.layout.simple_spinner_dropdown_item);//設置Spinner當中每個條目的樣式

2.得到Spinner對象,並設置數據:

spinner=(spinner)findViewById(R.id.spinnerId);

spinner.setAdapter(adapter);

spinner.setPrompt("測試");//標題

3.創建監聽器

class SpinnerOnSelectListener implements OnItemSelectedListener{

@override

public void onItemSelected(

AdapterView<?> adapterView,//整個列表對象

View view,//被選中的具體條目對象

int position,//位置

long id){ //id

String selected = adapterView.getItemAtPosition(position).toString();

}

@override

public void onNothingSelected(AdapterView<?> adapterView){

S.o.p("nothingSelected");

}

}

4.綁定監聽器

spinner.setOnItemSelectedListener(new SpinnerOnSelectListener());

註:第二種動態設計

1.創建ArrayList對象

List<String> list = new ArrayList<String>();

list.add("test1");

2. 調用方法

ArrayAdapter adapter = new ArrayAdapter(

this, //指上下文對象

R.layout.item, //引用了指定了下拉菜單的自定義布局文件

R.id.textViewId,//id

list);//數據

3.得到Spinner對象,並設置對象

spinner.setAdapter(adapter);

spinner.setPrompt("測試");//標題

3.創建監聽器

class SpinnerOnSelectListener implements OnItemSelectedListener{

@override

public void onItemSelected(

AdapterView<?> adapterView,//整個列表對象

View view,//被選中的具體條目對象

int position,//位置

long id){ //id

String selected = adapterView.getItemAtPosition(position).toString();

}

@override

public void onNothingSelected(AdapterView<?> adapterView){

S.o.p("nothingSelected");

}

}

4.綁定監聽器

spinner.setOnItemSelectedListener(new SpinnerOnSelectListener());

B.DatePicker和DatePickerDialog

1.聲明一個監聽器,使用匿名內部類

DatePickerDialog.OnDateSetListener onDateSetListener

= new DatePivkerDialog.OnDateSetListener(){

public void onDateSet(

DatePicker view,

int year,

int monthOfYear,

int dayOfMonth){

S.o.p(year+"-"+motnOfYear+"-"+dayOfMonth)

}

}

2.復寫onCreateDialog(int id)方法:

@override

protected Dialog onCreateDialog(int id){

switch(id){

case DATE_PICKER_ID:

return new DatePickerDialog(this,onDateSetListener,2019,11,25);

}

return null;

}

3.使用時調用showDialog()方法

showDialog(DATE_PICKER_ID);

C.AutoCompleteTextView

B.Widget

C.Animatin

十一、實現ContentProvider過程

1.定義一個CONTENT_URI常量

2.定義一個類,繼承ContentProvider

3.實現query、insert、update、delete、getType和onCreate方法

4.在AndroidManifest.xml當中進行聲明

E. android getvalueoftextsizeattr怎麼用

獲取arrt的值
有時候我們需要把顏色,數值寫成attr屬性,這樣做是為了屏蔽開發者對應具體數值,比如我們需要設置不同主題下的主色,副色,或者是不同版本的ActionBar大小,亦或者是不同Dpi下的DrawerLayout的寬度等。
在xml里,我們可以簡單的引用attr屬性值,例如:
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"

當然,我們有時候也需要在代碼中獲取attr屬性值:
TypedValue typedValue = new TypedValue();
context.getTheme().resolveAttribute(R.attr.yourAttr, typedValue, true);

// For string
typedValue.string
typedValue.coerceToString()

// For other data
typedValue.resourceId
typedValue.data;

獲取arrt樣式中的值
以上是針對個體數值根據不同類型來獲取的,如果想要獲取 style 的話,需要在拿到 resourceId 之後再進一步獲取具體數值,以 TextAppearance.Large 為例:
<style name="TextAppearance.Large">
<item name="android:textSize">22sp</item>
<item name="android:textStyle">normal</item>
<item name="android:textColor">?textColorPrimary</item>
</style>
TypedValue typedValue = new TypedValue();
context.getTheme().resolveAttribute(android.R.attr.textAppearanceLarge, typedValue, true);
int[] attribute = new int[] { android.R.attr.textSize };
TypedArray array = context.obtainStyledAttributes(typedValue.resourceId, attribute);
int textSize = array.getDimensionPixelSize(0 /* index */, -1 /* default size */);
array.recycle();

注意,要記得調用 TypedArray.recycle() 方法回收資源。

F. Android,如何在代碼中獲取attr屬性的值

如果使用的是jQuery,可以通過attr()方法獲取,比如獲取一個id為test的標簽的name值,使用代碼: $("#test").attr("name"); 這樣即可獲取到name屬性的值。

G. android attr怎麼用

attr一般都是定義一些全局的屬性,包括尺寸的信息還有一些字體的大小信息。

H. 【Android】自定義 View 中 defStyleAttr 的用法

在自定義 View 時,我們經常使用 AttributeSet 自定義一些布局參數。那 defStyleAttr 是如何使用的呢?

解釋說明:

Context context 是一個 View 的 Context,一般並不需要注意,但 View 是通過這個 Context 獲取主題 Theme, Resources 等資源的。可能涉及字體載入,語言問題,因此一定要注意當前的 Context。

AttributeSet attrs 是一個布局的屬性,在xml布局中的屬性都將在這個對象中。

int defStyleAttr 是當前主題中的一個屬性,它包含對樣式資源的引用,該樣式資源為 View 提供默認值。

int defStyleRes 是樣式資源的資源標識符,它為 View 提供默認值,僅在 defStyleAttr  = 0 或在主題中找不到時使用。

1、首先通過 context 獲得當前的主題 Theme

2、如果 defStyleAttr != 0,找到主題 Theme 中 defStyleAttr 的引用

3、查找該引用中是否有 itemHeight 參數

4、如果 defStyleAttr = 0 而且 defStyleRes != 0 ,找到 defStyleRes 中的 itemHeight 參數

步驟一,定義一個 attr 

步驟二,在自定義 View 中使用這個引用

步驟三,在Theme的Style中增加這個引用

步驟四,寫一個DefaultCustomViewStyle

I. android attr怎麼導入布局

自定義view簡單實現步驟
(1)首先創建attrs自定義屬性文件名稱,定義屬性以及相關數據類型
(2)再次創建自定義view,然後讀取相關屬性完成需要的view相關布局、繪制等工作
(3)最後在xml布局文件中引用或者直接在代碼中new一個相關對象進行使用

閱讀全文

與androidattr的使用相關的資料

熱點內容
汽車加密封條保溫嗎 瀏覽:564
安卓平板怎麼接hdmi信號 瀏覽:867
壓縮氣體的能量 瀏覽:836
北京市軟體u盤加密 瀏覽:681
php循環curl 瀏覽:319
安卓手機如何把桌面軟體放大 瀏覽:547
資料庫文件夾無法刪除 瀏覽:410
加密貨幣交易所成交量作假 瀏覽:337
打開應用加密時都有聲音 瀏覽:121
能打開java文件 瀏覽:786
志願伺服器怎麼使用 瀏覽:135
房貸解壓預約怎麼操作 瀏覽:200
蘋果下載app內購買怎麼弄 瀏覽:223
單片機按鍵的原理 瀏覽:423
安卓如何打開設置中的懸浮窗 瀏覽:603
擇吉日用什麼app 瀏覽:339
安卓轉蘋果王者什麼時間完成 瀏覽:816
linux文本編碼格式 瀏覽:666
浙江阿特拉斯壓縮機上門維修 瀏覽:631
自己創建的mc伺服器崩潰怎麼辦 瀏覽:160