當前位置:首頁 » 安卓系統 » android控制項初始化

android控制項初始化

發布時間: 2024-10-16 03:52:50

① 從源碼中淺析Android中怎麼利用attrs和styles定義控制項

1.attrs.xml:
我們知道Android的源碼中有attrs.xml這個文件,這個文件實際上定義了所有的控制項的屬性,就是我們在布局文件中設置的各類屬性
你可以找到attrs.xml這個文件,打開它,全選,右鍵->Show In->OutLine。可以看到整個文件的解構

我們大概可以看出裡面是Android中的各種屬性的聲明,比如textStyle這個屬性是這樣定義的:
java代碼
<!-- Default text typeface style. -->
<attr name="textStyle">
<flag name="normal" value="0" />
<flag name="bold" value="1" />
<flag name="italic" value="2" />
</attr>
那麼現在你知道,我們在寫android:textStyle的時候為什麼會出現normal,bold和italic這3個東西了吧,就是定義在這個地方。
再看看textColor:
Java代碼
<!-- Color of text (usually same as colorForeground). -->
<attr name="textColor" format="reference|color" />
format的意思是說:這個textColor可以以兩種方式設置,要麼是關聯一個值,要麼是直接設置一個顏色的RGB值,這個不難理解,因為我們可以平時也這樣做過。

也就是說我們平時在布局文件中所使用的各類控制項的屬性都定義在這裡面,那麼這個文件,除了定義這些屬性外還定義了各種具體的組件,比如TextView,Button,SeekBar等所具有的各種特有的屬性
比如SeekBar:

Java代碼
<declare-styleable name="SeekBar">
<!-- Draws the thumb on a seekbar. -->
<attr name="thumb" format="reference" />
<!-- An offset for the thumb that allows it to extend out of the range of the track. -->
<attr name="thumbOffset" format="dimension" />
</declare-styleable>
也許你會問SeekBar的background,等屬性怎麼沒有看到?這是因為Android中幾乎所有的組件都是從View中繼承下來的,SeekBar自然也不例外,而background這個屬性幾乎每個控制項都有,因此被定義到了View中,你可以在declare-styleable:View中找到它。

總結下,也就是說attrs.xml這個文件定義了布局文件中的各種屬性attr:***,以及每種控制項特有的屬性declare-styleable:***

2.styles.xml:
剛才的attrs.xml定義的是組件的屬性,現在要說的style則是針對這些屬性所設置的值,一些默認的值。

這個是SeekBar的樣式,我們可以看到,這裡面設置了一個SeekBar的默認的樣式,即為attrs.xml文件中的各種屬性設置初始值
Java代碼
<style name="Widget.SeekBar">
<item name="android:indeterminateOnly">false</item>
<item name="android:progressDrawable">@android:drawable/progress_horizontal</item>
<item name="android:indeterminateDrawable">@android:drawable/progress_horizontal</item>
<item name="android:minHeight">20dip</item>
<item name="android:maxHeight">20dip</item>
<item name="android:thumb">@android:drawable/seek_thumb</item>
<item name="android:thumbOffset">8dip</item>
<item name="android:focusable">true</item>
</style>
這個是Button的樣式:
Java代碼
<style name="Widget.Button">
<item name="android:background">@android:drawable/btn_default</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
<item name="android:textAppearance">?android:attr/textAppearanceSmallInverse</item>
<item name="android:textColor">@android:color/primary_text_light</item>
<item name="android:gravity">center_vertical|center_horizontal</item>
</style>

有了屬性和值,但是這些東西是如何關聯到一起的呢?它們如何被android的framework層所識別呢?

3.組件的源碼
我們看下TextView的源碼:
Java代碼
public TextView(Context context) {
this(context, null);
}//這個構造器用來給用戶調用,比如new TextView(this);

public TextView(Context context,
AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.textViewStyle);
}

public TextView(Context context,
AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);//為用戶自定義的TextView設置默認的style
mText = "";

//設置畫筆
mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
mTextPaint.density = getResources().getDisplayMetrics().density;
mTextPaint.setCompatibilityScaling(
getResources().getCompatibilityInfo().applicationScale);

mHighlightPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mHighlightPaint.setCompatibilityScaling(
getResources().getCompatibilityInfo().applicationScale);

mMovement = getDefaultMovementMethod();
mTransformation = null;

//attrs中包含了這個TextView控制項在布局文件中定義的屬性,比如android:background,android:layout_width等
//com.android.internal.R.styleable.TextView中包含了TextView中的針對attrs中的屬性的默認的值
//也就是說這個地方能夠將布局文件中設置的屬性獲取出來,保存到一個TypeArray中,為這個控制項初始化各個屬性
TypedArray a =
context.obtainStyledAttributes(
attrs, com.android.internal.R.styleable.TextView, defStyle, 0);

int textColorHighlight = 0;
ColorStateList textColor = null;
ColorStateList textColorHint = null;
ColorStateList textColorLink = null;
int textSize = 15;
int typefaceIndex = -1;
int styleIndex = -1;

/*
* Look the appearance up without checking first if it exists because
* almost every TextView has one and it greatly simplifies the logic
* to be able to parse the appearance first and then let specific tags
* for this View override it.
*/
TypedArray appearance = null;
//TextView_textAppearance不太了解為什麼要這樣做?難道是為了設置TextView的一些默認的屬性?
int ap = a.getResourceId(com.android.internal.R.styleable.TextView_textAppearance, -1);
if (ap != -1) {
appearance = context.obtainStyledAttributes(ap,
com.android.internal.R.styleable.
TextAppearance);
}
if (appearance != null) {
int n = appearance.getIndexCount();
for (int i = 0; i < n; i++) {
int attr = appearance.getIndex(i);

switch (attr) {
case com.android.internal.R.styleable.TextAppearance_textColorHighlight:
textColorHighlight = appearance.getColor(attr, textColorHighlight);
break;

case com.android.internal.R.styleable.TextAppearance_textColor:
textColor = appearance.getColorStateList(attr);
break;

case com.android.internal.R.styleable.TextAppearance_textColorHint:
textColorHint = appearance.getColorStateList(attr);
break;

case com.android.internal.R.styleable.TextAppearance_textColorLink:
textColorLink = appearance.getColorStateList(attr);
break;

case com.android.internal.R.styleable.TextAppearance_textSize:
textSize = appearance.getDimensionPixelSize(attr, textSize);
break;

case com.android.internal.R.styleable.TextAppearance_typeface:
typefaceIndex = appearance.getInt(attr, -1);
break;

case com.android.internal.R.styleable.TextAppearance_textStyle:
styleIndex = appearance.getInt(attr, -1);
break;
}
}

appearance.recycle();
}
//各類屬性
boolean editable = getDefaultEditable();
CharSequence inputMethod = null;
int numeric = 0;
CharSequence digits = null;
boolean phone = false;
boolean autotext = false;
int autocap = -1;
int buffertype = 0;
boolean selectallonfocus = false;
Drawable drawableLeft = null, drawableTop = null, drawableRight = null,
drawableBottom = null;
int drawablePadding = 0;
int ellipsize = -1;
boolean singleLine = false;
int maxlength = -1;
CharSequence text = "";
CharSequence hint = null;
int shadowcolor = 0;
float dx = 0, dy = 0, r = 0;
boolean password = false;
int inputType = EditorInfo.TYPE_NULL;

int n = a.getIndexCount();
for (int i = 0; i < n; i++) {
int attr = a.getIndex(i);
//通過switch語句將用戶設置的,以及默認的屬性讀取出來並初始化
switch (attr) {
case com.android.internal.R.styleable.TextView_editable:
editable = a.getBoolean(attr, editable);
break;

case com.android.internal.R.styleable.TextView_inputMethod:
inputMethod = a.getText(attr);
break;

case com.android.internal.R.styleable.TextView_numeric:
numeric = a.getInt(attr, numeric);
break;

//更多的case語句...

case com.android.internal.R.styleable.TextView_textSize:
textSize = a.getDimensionPixelSize(attr, textSize);//設置當前用戶所設置的字體大小
break;

case com.android.internal.R.styleable.TextView_typeface:
typefaceIndex = a.getInt(attr, typefaceIndex);
break;
//更多的case語句...
}

通過上面的代碼大概可以知道,每個組件基本都有3個構造器,其中只傳遞一個Context上下文的那個構造器一般用來在java代碼中實例化使用。
比如你可以
Java代碼
TextView tv = new TextView(context);
來實例化一個組件。

最終調用的是第3個構造器
Java代碼
public TextView(Context context,
AttributeSet attrs,
int defStyle)

在這個構造器中為你設置了默認的屬性attrs和值styles。關鍵不在這里,而是後面通過使用下面的代碼
Java代碼
TypedArray a =
context.obtainStyledAttributes(
attrs, com.android.internal.R.styleable.TextView, defStyle, 0);
來將屬性和值獲取出來,放到一個TypeArray中,然後再利用一個switch語句將裡面的值取出來。再利用這些值來初始化各個屬性。這個View最終利用這些屬性將這個控制項繪制出來。
如果你在布局文件中定義的一個View的話,那麼你定義的值,會被傳遞給構造器中的attrs和styles。也是利用同樣的方式來獲取出你定義的值,並根據你定義的值來繪制你想要的控制項。
再比如其實Button和EditText都是繼承自TextView。看上去兩個控制項似乎差異很大,其實不然。Button的源碼其實相比TextView變化的只是style而已:

② android控制項大全(詳細介紹常用的UI控制項及使用方法)

Android控制項是Android應用中最基本的組成部分之一,它們可以幫助我們構建用戶界面並實現應用程序的各種功能。在本文中,我們將介紹一些常用的AndroidUI控制項及其使用方法。

TextView

TextView是Android中最基本的控制項之一,它用於顯示文本。要在應用程序中使用TextView,可以按照以下步驟進行操作:

1.在XML布局文件中添加TextView控制項:

```

android:id="@+id/textView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="HelloWorld!"/>

```

2.在Java代碼中獲取TextView控制項的引用:

```

TextViewtextView=findViewById(R.id.textView);

```

3.設置TextView控制項的文本內容:

```

textView.setText("HelloAndroid!");

```

Button

Button是Android中常用的控制項之一,它用於響應用戶的點擊事件。要在應用程序中使用Button,可以按照以下步驟進行操作:

1.在XML布局文件中添加Button控制項:

```

android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Clickme!"/>

```

2.在Java代碼中獲取Button控制項的引用:

```

Buttonbutton=findViewById(R.id.button);

```

3.設置Button控制項的點擊事件:

```

button.setOnClickListener(newView.OnClickListener(){

@Override

publicvoidonClick(Viewv){

//在這里編寫點擊事件的處理代碼

}

});

```

ImageView

ImageView是Android中用於顯示圖片的控制項之一,它可以顯示來自資源文件或網路的圖片。要在應用程序中使用ImageView,可以按照以下步驟進行操作:

1.在XML布局文件中添加ImageView控制項:

```

android:id="@+id/imageView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/my_image"/>

```

2.在Java代碼中獲取ImageView控制項的引用:

```

ImageViewimageView=findViewById(R.id.imageView);

```

3.設置ImageView控制項的圖片源:

```

imageView.setImageResource(R.drawable.my_image);

```

EditText

EditText是Android中用於輸入文本的控制項之一,它可以讓用戶輸入文本並將其發送到應用程序中。要在應用程序中使用EditText,可以按照以下步驟進行操作:

1.在XML布局文件中添加EditText控制項:

```

android:id="@+id/editText"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Entertexthere"/>

```

2.在Java代碼中獲取EditText控制項的引用:

```

EditTexteditText=findViewById(R.id.editText);

```

3.獲取EditText控制項中的文本內容:

```

Stringtext=editText.getText().toString();

```

CheckBox

CheckBox是Android中用於選擇一個或多個選項的控制項之一,它可以讓用戶從多個選項中進行選擇。要在應用程序中使用CheckBox,可以按照以下步驟進行操作:

1.在XML布局文件中添加CheckBox控制項:

```

android:id="@+id/checkBox"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Checkme!"/>

```

2.在Java代碼中獲取CheckBox控制項的引用:

```

CheckBoxcheckBox=findViewById(R.id.checkBox);

```

3.獲取CheckBox控制項的選中狀態:

```

booleanisChecked=checkBox.isChecked();

```

RadioButton

RadioButton是Android中用於選擇一個選項的控制項之一,它可以讓用戶從多個選項中選擇一個。要在應用程序中使用RadioButton,可以按照以下步驟進行操作:

1.在XML布局文件中添加RadioButton控制項:

```

android:id="@+id/radioButton1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Option1"/>

android:id="@+id/radioButton2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Option2"/>

```

2.在Java代碼中獲取RadioButton控制項的引用:

```

RadioButtonradioButton1=findViewById(R.id.radioButton1);

RadioButtonradioButton2=findViewById(R.id.radioButton2);

```

3.獲取RadioButton控制項的選中狀態:

```

booleanisChecked1=radioButton1.isChecked();

booleanisChecked2=radioButton2.isChecked();

```

Spinner

Spinner是Android中用於選擇一個選項的控制項之一,它可以讓用戶從多個選項中選擇一個。要在應用程序中使用Spinner,可以按照以下步驟進行操作:

1.在XML布局文件中添加Spinner控制項:

```

android:id="@+id/spinner"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:entries="@array/my_options"/>

```

2.在Java代碼中獲取Spinner控制項的引用:

```

Spinnerspinner=findViewById(R.id.spinner);

```

3.獲取Spinner控制項的選中項:

```

StringselectedItem=spinner.getSelectedItem().toString();

```

Conclusion

本文介紹了一些常用的AndroidUI控制項及其使用方法,包括TextView、Button、ImageView、EditText、CheckBox、RadioButton和Spinner。通過學習這些控制項,您可以更好地構建Android應用程序的用戶界面,並實現各種功能。希望這篇文章對您有所幫助!

③ android中xml中有些控制項的屬性裡面有 "app:.." ,此處的app:是什麼意思和一般的android:有什麼區別

區別是:這兩個是聲明的不同的命名空間,android的是系統的,app是自定義的。


Android自定義控制項的屬性,在xml中使用自己自定義的attr的時候,其中有一步就是要自定義一個xml的命名空間後然後再給自定義屬性賦值,現在發現不知道什麼時候開始Android把這個改了,現在發現可以統一用
xmlns:app="http://schemas.android.com/apk/res-auto"
而不是原來的:
xmlns:app="http://schemas.android.com/apk/App的Package名"
還有人提到在作為lib被使用的時候,也應該用res-auto

所以說區別就是如果你http://schemas.android.com/apk/後面寫的是res/包名,那就是相關包名下的自定義屬性,而res-auto就是所有的自定義包名。

④ Edraw Max怎麼製作Android面板控制項

Android手機是經常使用的手機,隨著Android系統越來越受到歡迎,使用越來越廣泛。Edraw Max設計軟體自帶很多Android UI模型,可以利用這些設計出不同的Android模型,操作如下:

1、雙擊打開Edraw Max設計軟體,新建線框圖,如下圖所示:

2、在預定義模板和例子中選擇線框圖, 選擇Android UI,點擊創建圖標,如下圖所示:

3、新建空白的畫布,左側有Android UI、矢量圖標和觸摸手勢,如下圖所示:

4、在Android UI中的安卓手機框,拖曳一個垂直方向的手機框,如下圖所示:

5、找一個狀態欄2,拖曳到畫布上,並調整手機適應度,如下圖所示:

6、找到鍵盤控制,拖曳一個到手機框下方,使其適應手機框,如下圖所示:

⑤ Android之自定義控制項

一、簡單自定義控制項MyButton



每一個控制項都是一個java類,有對應的代碼,只要你能正確的編寫java代碼,那麼電腦培訓發現可以創造出符合你需求的控制項,即自定義控制項。


1.通過繼承的方式,創建自定義控制項


通過繼承一個現有的控制項,覆蓋其界面的呈現


通過繼承一個包含若乾子控制項的布局


通過繼承一個現有的控制項,覆蓋某個響應事件


繼承一個View來完整自定義一個心控制項


2.使你的自定義控制項繼承自某個最接近的Android控制項,必須是public


一般都會調用父類的構造方法,注意一般有三個構造方法


覆蓋原來控制項的方法,注意是否要再調用super中的方法


在XML中以類全名的方式引用此控制項


二、復雜自定義控制項MyLogin


需要設計包含一組控制項的自定義控制項就需要用到復雜的自定義控制項


1)使得你的自定義控制項繼承自某個接近的布局


2)正確的實現構造方法:構造方法中實例化目標布局,同時查找到各個子布局


3)添加相應的響應代碼來修改屬性,使得外部能訪問布局中的子控制項


4)在XML中以類全名的方式引用此控制項,完整的包名+類名。


熱點內容
ftp匿名登錄名為 發布:2024-10-16 08:08:07 瀏覽:278
安卓手機為什麼關不了屏幕 發布:2024-10-16 08:06:24 瀏覽:650
法師需要什麼配置 發布:2024-10-16 07:57:34 瀏覽:546
查詢資料庫用戶的許可權 發布:2024-10-16 07:51:18 瀏覽:438
安卓flv播放器哪個好 發布:2024-10-16 07:45:46 瀏覽:595
預演算法的類型 發布:2024-10-16 07:42:04 瀏覽:315
王者榮耀安卓哪個平台好 發布:2024-10-16 07:20:34 瀏覽:105
android獲取屏幕解析度 發布:2024-10-16 07:20:32 瀏覽:589
cf手游腳本文件 發布:2024-10-16 07:11:37 瀏覽:662
python35pygame 發布:2024-10-16 07:05:35 瀏覽:923