導航:首頁 > 操作系統 > android字體樣式xml

android字體樣式xml

發布時間:2024-09-15 16:25:32

android如何適配不同解析度

Android應用如何適配不同解析度的手機,主要分三塊考慮:
1.界面配置,根據不同的解析度,創建手機界面文件
例子:
在res下創建 layout-800x480,layout-480x320,並在各自不同解析度的文件夾下創建界面文件
2.圖片配置,不同的解析度,界面的長寬比不一致,需要不同規格的圖片,在drawable-hdpi,drawable-ldpi,drawable-mdpi 中放不同解析度的圖片.
注:為了減小整個應用程序安裝包大小,選用最高解析度適配,特殊界面圖片特殊處理.
3.動態實現的界面,樣式的設定,不同解析度,界面的字體大小,字體等需要不同的樣式,且需要動態生成的情況下,需要把不同解析度的配置信息保存到應用中。
例子:
在RES里創建 values-480x320 values-800x400 value-1280x720,並在創建的文件夾中分別創建dimens.xml,<dimen name="Text_size">30px</dimen>,在程序中直接調R.dimen.Text_sizeint sizeOfText = (int) this.getResources().getDimension(R.dimen.Text_size);
注:實際應用發現,字體大小適配時,比如只適配了如下屏幕字體
values-480x320 values-800x480 value-1280x720當出現手機屏幕解析度為 854x480時會自動找最大字體適配 (value-1280x720).

總結:如果字體800x480以上沒有適配,手機自動按最大解析度適配.

② Android仿微信全局字體大小調整

最近項目添加了一項調整應用字體大小功能,做完後空閑之餘總結一下。本功能仿照微信應用「設置」 - 「通用」 - 「字體大小」功能,又有一點區別。據我所知,常見改變全局字體大小方法有兩種,我把這兩種分為可控和不可控,為什麼這么分呢,當然不是為了方便記憶。那麼簡單說下兩者方式的實現過程:

1、不可控:通過重寫Actiivity的getResources()方法更新應用的字體倍數來調整全局字體大小

2、可控:通過setTheme()方法,一開始就初始化設置不同風格的字體樣式來更改全局字體大小。

而本文正式採用了第一種方案,主要是中途添加該功能,時間也不充裕,抽取字體大小又太過耗時。

微信字體大小個人猜測使用第二種方案,後者是更好的實現方式也不一定。

xml使用方式:

2、滑動按鈕改變當前頁面預覽字體大小

3、返回時,保存放大倍數並重啟應用

4、初始化應用時配置字體放大倍數。

源碼地址:
https://github.com/DayorNight/BLCS
到這里就結束啦。

③ 從源碼中淺析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布局優化的幾種方式

1. include/merge

布局優化中常常用到include/merge標簽,include的含義類似C代碼中的include,意思是直接把指定布局片段包含進當前的布局文件。include適用於多個布局文件中存在相同的xml片段,比如說相同的標題欄、相同的廣告欄、相同的進度欄等等。

2. ViewStub

在一個頁面上根據不同條件展示不同的控制項,我們常常會設置控制項的可視屬性,比如調用指定控制項的setVisibility方法,若需展示則設置View.VISIBLE,若需隱藏則設置View.GONE。不過gone的控制項只是看不到罷了,實際UI渲染時還是會被載入。要想事先不載入,在條件符合時才載入,就得用到標簽ViewStub。

3. style樣式

樣式在res/values/styles.xml中定義,它適用於下面幾種情況:
1、布局文件中存在多個具有相同風格的控制項,比如說統一的文本框TextView,都是白底黑字、中號字體、居中顯示,這時我們便可在styles.xml定義一種文本樣式,然後在各文本框處聲明它的style屬性。好處一個是減少了布局文件的大小,另一個是方便以後統一修改風格。

2、某些控制項在代碼中聲明時需要手工指定style,例如自定義對話框需要在構造函數中指定樣式;另一個例子是彈窗PopupWindow在設置伸縮動畫方法setAnimationStyle時需要指定動畫樣式。
3、定義頁面的主題風格,然後應用到Activity頁面。代碼中設置主題可通過「setTheme(R.style.)」完成,布局中設置可在AndroidManifest.xml的activity節點下添加theme屬性,如「android:theme=」@style/「」。

4. Theme主題

主題是一種特殊的樣式,主題專用於頁面,而樣式一般運用於控制項。主題定義一般放在themes.xml,樣式定義一般放在styles.xml。
Android定義了一些系統主題,完整定義的參見sdk自帶的themes.xml,常用的幾種說明如下:
Theme.NoTitleBar : 不顯示標題欄,即隱藏ActionBar
Theme.Light : 白色背景
Theme.Holo : 淺灰背景
Theme.Black : 黑色背景
Theme.Wallpaper : 壁紙
Theme.Translucent : 透明背景
Theme.Dialog : 對話框
Theme.Panel : 平板
Theme.InputMethod : 輸入法
Theme.SearchBar : 搜索框

閱讀全文

與android字體樣式xml相關的資料

熱點內容
手機proxy伺服器地址 瀏覽:449
吉他清音壓縮 瀏覽:301
簡歷模板程序員 瀏覽:881
螺桿壓縮機虛標型號 瀏覽:953
idea開發項目伺服器ip地址 瀏覽:125
串口伺服器出現亂碼怎麼解決 瀏覽:950
命令按鈕的default 瀏覽:161
戰網如何登錄其他伺服器 瀏覽:990
中國銀行app如何關閉簡訊 瀏覽:493
nx120編程技巧 瀏覽:722
手機也能使用源碼公式 瀏覽:918
怎樣把壓縮的文件下載 瀏覽:334
pdf是哪的 瀏覽:27
群暉伺服器如何建立自己資料庫 瀏覽:868
win10怎麼查找伺服器地址 瀏覽:506
freepdfsplit 瀏覽:172
如何更改linux伺服器地址 瀏覽:221
編程求字元串abcdefh長度 瀏覽:312
座機時間伺服器地址 瀏覽:419
華康寶app是怎麼樣的 瀏覽:73