2011-09-07 75 views
4

我試圖在一個按鈕內使用兩個不同字體的TextView自定義按鈕。對於我只是延伸按鈕,並與已經寫在構造函數中下面的代碼,與兩個TextView的自定義按鈕

LayoutInflater layoutInflater = (LayoutInflater) context 
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View layout = layoutInflater.inflate(R.layout.custom_button, 
     (ViewGroup) findViewById(R.id.custom_button_view)); 

TextView firstTextView = (TextView) layout 
     .findViewById(R.id.firstTextView); 
TextView secondTextView = (TextView) layout 
     .findViewById(R.id.secondTextView); 

佈局custom_button我兩次的TextView用不同的字體和文字custom_button_view是LinearLayout中的ID,我得到了什麼是一個沒有文字的空按鈕。

任何想法,謝謝。

+0

http://stackoverflow.com/questions/2973270/using-a-custom-typeface -in-android 看看那個;它擁有一切。 – Hades

+0

custom_button的xml佈局是什麼樣的?你在那裏設置文字? –

+0

是的,我在那裏設置了文字。 – Vignesh

回答

6

您可以使用佈局按鈕設置烏爾自定義按鈕的風格佈局,並可以添加兩個textViews給它,這樣:

<LinearLayout android:id="@+id/customButtonLayout" 
    android:layout_height="wrap_content" style="@android:style/Widget.Button" 
    android:layout_width="wrap_content"> 
    <TextView android:text="First" android:id="@+id/firstTextView" 
     android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:textColor="#000"></TextView> 
    <TextView android:textColor="#000" android:text="Second" 
     android:layout_height="wrap_content" android:id="@+id/secondTextView" 
     android:layout_width="wrap_content" android:layout_marginLeft="10dp"></TextView> 
</LinearLayout> 

,並在活動中,你可以有這樣的設置不同的字體:

Typeface font=Typeface.createFromAsset(getAssets(),"ARIALN.TTF") ; 
    Typeface font2=Typeface.createFromAsset(getAssets(), "COMPCTAN.TTF"); 

    TextView firstTextView = (TextView)findViewById(R.id.firstTextView); 
    TextView secondTextView = (TextView)findViewById(R.id.secondTextView); 

    firstTextView.setTypeface(font); 
    secondTextView.setTypeface(font2); 

    LinearLayout btnLayout=(LinearLayout) findViewById(R.id.customButtonLayout); 
    btnLayout.setOnClickListener(this); 
2

您可以從Button派生一個新的類並覆蓋onDraw(Canvas canvas)方法。我做了一個帶有圖標和文本的按鈕,它沒有任何xml。主要的問題是將文本寫在按鈕上的好位置。爲此,您可以使用Paint.getTextBounds()函數來獲取文本尺寸。

使用LayoutInflater可能是一種更好的做法,但我沒有設法使其工作。

public class CustomButton extends Button { 

    private int  mWidth; 
    private int  mHeight; 
    private Bitmap mBitmap; 
    private String mText; 

    private Paint mPaintIcon; 
    private Rect mRectIconSrc; 
    private Rect mRectIconDst; 
    private Paint mPaintText; 

    public CustomButton(Context context, Bitmap bitmap, int width, int height, String text) { 
     super(context); 

     mBitmap = bitmap; 
     mWidth = width; 
     mHeight = height; 
     mText = text; 
     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height); 
     setLayoutParams(params); 

     mPaintIcon = new Paint(); 
     mRectIconSrc = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight()); 
     mRectIconDst = new Rect(0, 0, mHeight, mHeight); 

     mPaintText = new Paint(); 
     mPaintText.setColor(0xFF778800); 
     mPaintText.setTextSize(30); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 

     canvas.drawBitmap(mBitmap, mRectIconSrc, mRectIconDst, mPaintIcon); 
     canvas.drawText(mText, mWidth/4, mHeight*2/3, mPaintText); 
    } 

} 
0

可以環繞buttonFrameLayout,然後FrameLayout中添加一個textview。您可以在活動中管理字體。如果使用該文本不顯示嘗試bringToFront()

佈局:

<FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:layout_constraintLeft_toLeftOf="parent" 
      app:layout_constraintRight_toRightOf="parent" 
      android:id="@+id/button_frame" 
      > 
     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/button_border" 
      android:gravity="center" 
      android:onClick="onClick" 
      android:text="@string/get_more" 
      android:id="@+id/get_more" 
      android:stateListAnimator="@null" 

     /> 

      <TextView 
       android:id="@+id/linearTimer" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:gravity="center" 
       android:layout_gravity="right" 
       android:padding="10dp" 
       android:text="123" 
      > 
    </FrameLayout> 

活動:

countDownView = (TextView) findViewById(R.id.linearTimer); 
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/digital-7.ttf"); 
countDownView.setTypeface(tf); 
countDownView.bringToFront();