2013-09-30 46 views
0

我正在研究一個應用程序,並且我在主菜單上有一系列按鈕,我希望它們都具有相同的文本大小。我可以在屬性文件中指定它嗎?是否可以在XML中動態設置Android按鈕文本大小?

這是我目前的XML文件。

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
    <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
     <Button 
       android:id="@+id/sensors_available" 
       android:text="@string/sensors_available" 
       android:textSize="12sp" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"/> 
    </TableRow> 
    <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
     <Button 
       android:id="@+id/b_start_hm" 
       android:text="@string/hm_start_service" 
       android:textSize="12sp" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"/> 
     <Button 
       android:id="@+id/b_stop_hm_service" 
       android:text="@string/hm_stop_service" 
       android:textSize="12sp" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"/> 
    </TableRow> 
    <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
     <Button 
       android:id="@+id/b_start_hot_video" 
       android:text="@string/video_service_start" 
       android:onClick="startHotVideoService" 
       android:textSize="12sp" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"/> 
     <Button 
       android:id="@+id/b_hv_stop_service" 
       android:text="@string/hv_stop_service" 
       android:textSize="12sp" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"/> 
    </TableRow> 
</TableLayout> 

是否有可能對我來說,替換所有12sp喜歡的東西@string\custom_text_size,然後定義在string.xml文件?當我嘗試這個時,我開始我的應用程序時遇到了一個致命異常。

+1

u可以使用捫... 看到更多此處獲得更多信息 http://stackoverflow.com/questions/11121028/load-dimension-value-from-res-values-dimension-xml-from-source-code –

回答

2

你是如此接近,但你需要定義一個dimen而不是string

<dimen name="textSize12">12sp</dimen> 

和你Button看起來像this.-

<Button 
    android:id="@+id/b_stop_hm_service" 
    android:text="@string/hm_stop_service" 
    android:textSize="@dimen/textSize12" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 

理想的情況下,dimen資源定義一個單獨的資源文件,如dimens.xml

0

Crea TE在res /價值/ styles.xml風格:

<style name="TextSize"> 
    <item name="android:textSize">12sp</item> 
</style> 

聲明它在你的XML:

<Button 
    android:id="@+id/b_start_hot_video" 
    android:text="@string/video_service_start" 
    android:onClick="startHotVideoService" 
    style="@style/TextSize" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 

通過使用樣式,您可以存儲多種設置,如可繪製背景,文字顏色等等......

+0

PS:恕我直言,沒有必要創建一個風格只是爲了設置'textSize'。除非你設置了更多的屬性(比如顏色,陰影,邊距等),否則使用'@dimen'資源對我來說更有意義。 – ssantos

+0

同意@ssantos,我只是想給一個更靈活的答案。 –

相關問題