创建自定义按钮,两行文字,每一个不同的字体 [英] Creating a custom button with two lines of text, each with a different font

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

问题描述

我有点卡住这里真的可以使用一些帮助。在我看来,它应该是pretty的易文两个独立的行添加到一个按钮。但它只是不。有一种方法可以使用​​HTML标签做到这一点,但是,这并不让你指定的字体或超出了文字大小大和小。

下面是我的按钮,这就是所谓的clicky:

 < XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目机器人:可绘制=@可绘制/的buttonDown
          机器人:STATE_ pressed =真/>
    <项目机器人:可绘制=@可绘制/ buttonup/>
< /选择器>
 

和这里的地方它显示在布局文件之一:

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
 机器人:方向=垂直
 机器人:layout_width =FILL_PARENT
  机器人:layout_height =FILL_PARENT>



<按钮
        机器人:ID =@ + ID / clicky
        机器人:layout_width =137.5dip
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentRight =真
        机器人:layout_centerHorizo​​ntal =真
        机器人:layout_marginTop =2DIP
        机器人:layout_marginBottom =2DIP
        机器人:layout_marginRight =1dip
        机器人:TEXTSIZE =20dip
        机器人:背景=@可绘制/ clicky
  />


  <的ListView
    机器人:ID =@ + ID / ListView01
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_width =FILL_PARENT
    机器人:分隔=#000000
    机器人:dividerHeight =2DIP
    机器人:layout_below =@ + ID /分隔符/>

< / RelativeLayout的>
 

和这里就是我在OnCreate()方法:

 字样CustomFont的= Typeface.createFromAsset(getAssets(),字体/ zooper.ttf);
 按钮mButton =(按钮)findViewById(R.id.clicky);
        mButton.setText(你好);
        TextView的clicky =(TextView中)findViewById(R.id.clicky);
        clicky.setTypeface(CustomFont的);
 

在按钮上的文本是动态创建的,所以我要在这里做(现在它是静态的,但后来的您好将被替换为一个变量)。文本的其他行会小得多,静置于下方的你好。我已经通过一切对谷歌走了,即使是远程类似我的问题,但我无法找到我能适应​​我的问题的答案。

所以再次,我需要两个单独的行文字,一个在另一个上方的单个按钮。最上面一行是大的,动态创建和使用自定义字体。文本的下行会小得多,静态和可以使用一个系统字体。

难道真的这么难只是一个窝里面的LinearLayout一个按钮?

任何帮助将是非常美联社preciated。

解决方案

 字符串S1;
字符串s2;
INT N = s1.length();
INT M = s2.length;

字样CustomFont的= Typeface.createFromAsset(getAssets(),字体/ zooper.ttf);
Spannable跨度=新SpannableString(S1 +\ N+ S2);
//大字体,直到你找到`\ N`
span.setSpan(新CustomTypefaceSpan(时,CustomFont),0,正,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//小字体从`\ N`到底
span.setSpan(新RelativeSizeSpan(0.8f)中,n,第(n + M + 1),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
yourButton.setText(跨度);
 

I'm a little stuck here and could really use some help. It seems to me that it should be pretty easy to add two separate lines of text to a button. But it just isn't. There's a way to do it with html tags, but that doesn't let you specify the font or the text size beyond "large" and "small".

Here's my button, it's called 'clicky':

   <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/buttondown"
          android:state_pressed="true" />
    <item android:drawable="@drawable/buttonup" />
</selector>

And here's where it shows up in one of the layout files:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" 
 android:layout_width="fill_parent"
  android:layout_height="fill_parent">



<Button
        android:id="@+id/clicky"
        android:layout_width="137.5dip"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="2dip"
        android:layout_marginBottom="2dip"
        android:layout_marginRight="1dip"
        android:textSize="20dip"
        android:background="@drawable/clicky"
  />


  <ListView 
    android:id="@+id/ListView01" 
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:divider="#000000"
    android:dividerHeight="2dip"
    android:layout_below="@+id/separator"/>

</RelativeLayout>

And here's what I have in the onCreate() method:

Typeface customFont = Typeface.createFromAsset(getAssets(),"fonts/zooper.ttf");
 Button mButton=(Button)findViewById(R.id.clicky);
        mButton.setText("Hi There");
        TextView clicky = (TextView)findViewById(R.id.clicky);
        clicky.setTypeface(customFont);

The text in the button is created dynamically so I have to do it from here (right now it's static, but later "Hi There" will be replaced with a variable). The other line of text will be much smaller, static and placed beneath "Hi There". I've gone through everything on Google that even remotely resembles my question but I just can't find an answer that I can adapt to my problem.

So once again, I need a single button with two separate lines of text, one above the other. The top line is large, dynamically created and uses a custom font. The lower line of text will be much smaller, static and can use a system font.

Is it really so hard just to nest a LinearLayout inside a button?

Any help would be much appreciated.

解决方案

String s1;
String s2;
int n = s1.length();
int m = s2.length;

Typeface customFont = Typeface.createFromAsset(getAssets(),"fonts/zooper.ttf");
Spannable span = new SpannableString(s1 + "\n" +  s2);
//Big font till you find `\n`
span.setSpan(new CustomTypefaceSpan("", customFont), 0, n, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//Small font from `\n` to the end
span.setSpan(new RelativeSizeSpan(0.8f), n, (n+m+1), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
yourButton.setText(span);

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

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