2010-05-13 45 views
28

我,一直在試圖創建使用本教程中的Android自定義按鈕 - http://www.gersic.com/blog.php?id=56的Android按鍵字體大小

它運作良好,但它並沒有說如何改變字體大小或權重。有任何想法嗎?

這裏還有一個問題,唯一的答案是使用html樣式,但是如果不使用css(或廢棄的字體標籤),則無法在html中更改字體大小。必須有更好的方法來設置按鈕上使用的字體的像素大小?

+0

查看工作示例http://javatechig.com/2013/03/19/using-external-fonts-in-android-view/ – 2013-04-07 20:58:30

回答

83

您在XML定義這些屬性,你會別的,例如:

<Button android:id="@+id/next_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/next" 
      android:background="@drawable/mybutton_background" 
      android:textSize="10sp" /> <!-- Use SP(Scale Independent Pixel) --> 

您可以找到api允許的屬性。或者,如果您希望將其應用於應用程序中的所有按鈕,請創建一個樣式。請參閱Styles and Themes開發文檔。

+11

不應將textsize設置爲SP而不是DP?請參閱http://stackoverflow.com/questions/2025282/difference-of-px-dp-dip-and-sp-in-android – 2012-02-22 21:09:48

+1

請使用SP而不是DP – 2014-04-04 13:42:31

7
Button butt= new Button(_context); 
butt.setTextAppearance(_context, R.style.ButtonFontStyle); 

和在res /值/ style.xml

<resources> 
    <style name="ButtonFontStyle"> 
      <item name="android:textSize">12sp</item> 
    </style> 
</resources> 
5

編程:

Button bt = new Button(this); 
bt.setTextSize(12); 

在XML:

<Button 
    android:textSize="10sp" 
/> 
2

我試圖把在字體大小styles.xml,但是當我去使用它時,它只是允許的從捫文件夾荷蘭國際集團的資源,以便把它放在那裏,而不是,不知道它這是正確的

 <Button 
       android:layout_weight="1" 
       android:id="@+id/three_btn" 
       android:layout_height="match_parent" 
       android:layout_width="0dp" 
       android:onClick="onButtonClick" 
       android:textColor="#EEEEEE" 
       android:textStyle="bold" 
       android:textSize="@dimen/buttonFontSize" 
       android:text="3"/> 
0
<resource> 
<style name="button"> 
<item name="android:textSize">15dp</item> 
</style> 
<resource> 
0

另一種編程的方法;

final Button btn = (Button) findViewById(R.id.btnSize); 

     final float[] size = {12}; 

     btn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       size[0] +=2; 
       btn.setTextSize(size[0] +2); 
      } 
     }); 

每當你點擊你的按鈕,按鈕文字將被改變(+ 2px大小)。您可以添加另一個按鈕並更改大小-2px。如果您想爲另一個開口節省大小,則可以使用共享偏好界面。