有两个TextView的自定义按钮 [英] Custom Button with two TextView

查看:168
本文介绍了有两个TextView的自定义按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有两个TextView的一个按钮内不同的字体自定义按钮。对于我只是扩展按钮,并写了下面的code的构造函数中,

I'm trying to Customize button with two TextView with different typeface within a single button. For that I just extended Button and with have written the following code inside the constructor,

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,我得到了什么是一个没有文字的空按钮。

in the layout custom_button I have placed two TextView with different typeface and text and custom_button_view is the ID of that LinearLayout, what I got is an empty button with no text.

任何想法,谢谢。

推荐答案

您可以使用布局设置UR自定义按钮样式按钮布局,可以添加两个textViews吧,是这样的:

You can use Layout as a button by setting ur custom button style to layout and can add two textViews to it, in this way:

<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>

和活动中,你可以有这个设置不同的字体:

and in Activity you can have this to set different typeface:

    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);

这篇关于有两个TextView的自定义按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆