自定义按钮,带有两个文本视图。

6
我正试图在一个按钮中使用具有不同字体的两个TextView自定义按钮。为此,我扩展了Button并在构造函数中编写了以下代码:
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 中,但是我得到的结果是一个没有文本的空按钮。
有任何想法,谢谢。

看看这个链接吧,它几乎包含了所有内容。 - Hades
自定义按钮的 XML 布局是什么样子的?你会在那里设置文本吗? - aj.esler
是的,我已经把文本放在那里了。 - Vignesh
3个回答

8

您可以通过将自定义按钮样式设置为布局并在其中添加两个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>

在Activity中,您可以使用以下代码设置不同的字体:

    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
您可以从按钮派生一个新类,并覆盖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

您可以使用FrameLayoutbutton包围起来,然后在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();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接