2011-02-02 36 views
21

是否可以在顯示文本的所有視圖中使用字體大小的應用程序範圍設置? 我想提供一個偏好給用戶,它應該允許縮放應用程序中的所有文本。Android:應用程序範圍的字體大小首選項

Android明確允許將"sp" dimension unit用於可縮放文本,但是沒有實際的方式來以全局方式設置「用戶的字體大小首選項」。

通過對活動的實例化所有視圖迭代是不是一個真正的選擇;-)

+0

可能的重複[從代碼動態更改多個textview的大小(沒有「在磁盤上」xml主題)?](http://stackoverflow.com/questions/4473397/dynamically-change-size-of-multiple- textview-from-code-without-a-on-disk-xml -t) – 2011-02-02 17:15:17

+0

這個問題已經被討論了很多次,參見http://stackoverflow.com/questions/4473397/dynamically-change-size-of- multiple-textview-from-code-without-a-on-disk-xml-t查看其他相關鏈接的列表。 – 2011-02-02 17:15:34

回答

46

這裏這是我爲我的應用程序做的。 用幾句話 - 在Activity.onCreate()中,您可以獲取具有特定字體大小的樣式的資源ID,並將此樣式應用於活動主題。然後通過首選項活動,您可以在這些組之間切換。

首先在值/ attrs。

<declare-styleable name="FontStyle"> 
    <attr name="font_small" format="dimension" /> 
    <attr name="font_medium" format="dimension" /> 
    <attr name="font_large" format="dimension" /> 
    <attr name="font_xlarge" format="dimension" /> 
</declare-styleable> 

然後在值/ styles.xml聲明幾集的字體大小:對於集的字體大小的XML聲明屬性

<style name="FontStyle"> 
</style> 

<style name="FontStyle.Small"> 
    <item name="font_small">14sp</item> 
    <item name="font_medium">16sp</item> 
    <item name="font_large">18sp</item> 
    <item name="font_xlarge">20sp</item> 
</style> 

<style name="FontStyle.Medium"> 
    <item name="font_small">18sp</item> 
    <item name="font_medium">20sp</item> 
    <item name="font_large">22sp</item> 
    <item name="font_xlarge">24sp</item> 
</style> 

<style name="FontStyle.Large"> 
    <item name="font_small">26sp</item> 
    <item name="font_medium">28sp</item> 
    <item name="font_large">30sp</item> 
    <item name="font_xlarge">32sp</item> 
</style> 
在每一個活動的 onCreate()方法

然後加入:

getTheme().applyStyle(new Preferences(this).getFontStyle().getResId(), true); 

其中Preferences是一個外觀SharedPreferences對象:

public class Preferences { 
    private final static String FONT_STYLE = "FONT_STYLE"; 

    private final Context context; 

    public Preferences(Context context) { 
     this.context = context; 
    } 

    protected SharedPreferences open() { 
     return context.getSharedPreferences("prefs", Context.MODE_PRIVATE); 
    } 

    protected Editor edit() { 
     return open().edit(); 
    } 

    public FontStyle getFontStyle() { 
     return FontStyle.valueOf(open().getString(FONT_STYLE, 
      FontStyle.Medium.name())); 
    } 

    public void setFontStyle(FontStyle style) { 
     edit().putString(FONT_STYLE, style.name()).commit(); 
    } 
} 

和FontStyle是:

public enum FontStyle { 
    Small(R.style.FontStyle_Small, "Small"), 
    Medium(R.style.FontStyle_Medium, "Medium"), 
    Large(R.style.FontStyle_Large, "Large"); 

    private int resId; 
    private String title; 

    public int getResId() { 
     return resId; 
    } 

    public String getTitle() { 
     return title; 
    } 

    FontStyle(int resId, String title) { 
     this.resId = resId; 
     this.title = title; 
    } 
} 

而且FontStyle.values()用作您的PreferencesActivity物品Spinner。 那是我的樣子:

public class PreferencesActivity extends Activity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    getTheme().applyStyle(new Preferences(this).getFontStyle().getResId(), true); 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.preferences); 

    Preferences prefs = new Preferences(this); 

    Spinner fontStylesView = (Spinner) findViewById(R.id.font_styles); 
    FontStylesAdapter adapter = new FontStylesAdapter(this, 
      R.layout.font_styles_row, FontStyle.values()); 
    fontStylesView.setAdapter(adapter); 

    fontStylesView.setSelection(prefs.getFontStyle().ordinal()); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getSupportMenuInflater(); 
    inflater.inflate(R.menu.preferences, menu); 
    return super.onCreateOptionsMenu(menu); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case R.id.menu_done: 
     onMenuDone(); 
     finish(); 
     return true; 
    case R.id.menu_cancel: 
     finish(); 
     return true; 
    default: 
     return false; 
    } 
} 

private void onMenuDone() { 
    Preferences prefs = new Preferences(this); 

    Spinner fontStylesView = (Spinner) findViewById(R.id.font_styles); 
    prefs.setFontStyle((FontStyle) fontStylesView.getSelectedItem()); 
} 
} 

最後,你可以用你的字體大小的喜好:

<TextView android:textSize="?attr/font_large" /> 

或者我更喜歡使用樣式,價值觀/ styles.xml補充:

<style name="Label" parent="@android:style/Widget.TextView"> 
    <item name="android:textSize">?attr/font_medium</item> 
    <item name="android:layout_width">wrap_content</item> 
    <item name="android:layout_height">wrap_content</item> 
</style> 

<style name="Label.XLarge"> 
    <item name="android:textSize">?attr/font_xlarge</item> 
</style> 

你可以這樣使用它:

<TextView style="@style/Label.XLarge" /> 

我希望我的回答能幫到你。

6

是的,這是可能的。要做到這一點,你需要:

  1. 聲明自己的類,在所有的對話/活動 延長TextView
  2. 只能使用它

像:

public class SimpleTextView extends TextView 
{ 
    private static final float DEFAULT_TEXT_SIZE=12.0; 
    private static float textSize=DEFAULT_TEXT_SIZE; 

    public SimpleTextView(Context context) 
    { 
     super(context); 
     this.setTextSize(textSize); 
    } 

    public SimpleTextView(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 
     this.setTextSize(textSize); 
    } 

    public SimpleTextView(Context context, AttributeSet attrs, int defStyle) 
    { 
     super(context, attrs, defStyle); 
     this.setTextSize(textSize); 
    } 

    public static void setGlobalSize(float size) 
    { 
     textSize=size; 
    } 

    public static float getGlobalSize() 
    { 
     return textSize; 
    } 
} 

而現在徘徊無論你可以在所有全文視圖中將全部文本大小全局更改爲20,只需撥打:

SimpleTextView.setGlobalTextSize(20); 
+4

這適用於出現在TextView中的文本,但是像EditText,Button,CheckedTextView等所有的TextView子類呢?你基本上需要創建你使用的每個Widget類型的子類。 – 2011-02-02 18:10:20

+0

@Mayra:那個正確的 - 我被迫這樣做......我的意思是它不是很有趣,但結果令人着迷:) – barmaley 2011-02-02 19:46:30

1

從這裏臀部拍攝的想法考慮(沒有自定義的TextView實施所需)

  1. 財產申報像UNIVERSAL_FONT_SIZE的想法,它可以從設置中更改,但應用程序調用之間將保留
  2. 在每個活動的onCreate方法獲取財產的價值,並保存爲一個場
  3. 讓你的代碼中使用,對於每個文本調整大小的組件
  4. 沒有將採取行動無法阻止您創建多個屬性,如BUTTONS_TXT_SIZE,TEXT_SIZE,LIST_TXT_SIZE等,然後具有邏輯,例如文本增加的百分比並爲每種控件計算適當的大小(因爲對於不同的控件可能有不同的大小)

沿着同樣的路線說,你想讓它動態工作嗎?創建一個簡單的類(說TextSetter)持有內部列表,並有3種方法:添加,刪除和的setSize

  1. Activity#onCreate確定要調整和使用TextSetter#set將其添加到列表中
  2. 當每個控制用戶想要從菜單中增加/減小字體大小,當你處理剛纔執行的TextSetter#setSize時,你將通過控制列表循環,檢測它是哪種類型並相應地調整文本大小
0

對於那些使用@mixels答案的自定義屬性來擴大視圖的問題。

如果您的視圖位於片段中,則還需要在片段的onCreateView()中應用FontStyle,或者在應用程序的主題中設置這些屬性的值。

更多詳情請參閱我的aswer here

相關問題