简体   繁体   English

如何在Android中使用两种不同的字体大小的按钮文字?

[英]How to use two different font sizes for button text in Android?

How could I implement a button background for ImageButton or Button which contains 2 textview each using different font sizes ? 我如何实现ImageButton或Button的按钮背景,其中包含2个textview,每个使用不同的字体大小? I cant use static images as background and I need to achieve this either using xml or programmatically. 我不能使用静态图像作为背景,我需要使用xml或以编程方式实现此目的。

This is a numeric keypad and it carries a numeric value and a set of alphabets each of which uses different font sizes and needs to be set as button text. 这是一个数字小键盘,它带有一个数字值和一组字母,每个字母使用不同的字体大小,需要设置为按钮文本。

在此输入图像描述

Here are some of the suggestions that pop-up on my brain but seems to have limitations since I want to reuse the UI component and avoid code repetition. 以下是我脑中弹出的一些建议但似乎有限制,因为我想重用UI组件并避免重复代码。

  1. Create a layout xml containing 2 textfields and set that as background on the button. 创建一个包含2个文本字段的布局xml,并将其设置为按钮上的背景。 But how can I get a reference of these textview fields to set the value on them in the MyActivity.java? 但是,如何获取这些textview字段的引用以在MyActivity.java中设置它们的值?

  2. Use a layeredlist ? 使用分层列表? still same problem reference of textviews 文本视图的问题参考仍然相同

  3. Or create a custom view and inflate layout mentioned in step 1. This solution resolves my problem. 或者创建第1步中提到的自定义视图和膨胀布局。此解决方案可以解决我的问题。

Does any other solution exist for this UI requirement? 此UI要求是否存在任何其他解决方案?

Thanks. 谢谢。

Hopefully this will help. 希望这会有所帮助。 This off course is not the only solution but it is a pretty simple one. 这个过程不是唯一的解决方案,但它非常简单。 Add the xml part in your xml instead of the button. 在xml中添加xml部分而不是按钮。

XML XML

<LinearLayout
    android:id="@+id/button_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp" 
        android:text="5"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="12sp" 
        android:text="ABC"/>

</LinearLayout>

CODE

    LinearLayout button= (LinearLayout)findViewById(R.id.button_layout);
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO DO whatever you want to do here

        }
    });

For your button you can use code like this 对于您的按钮,您可以使用这样的代码

don't forget put font in your Assets folder 不要忘记在Assets文件夹中输入字体

 Button btnA=(Button) findViewById(R.id.button1);
 Typeface typeface = Typeface.createFromAsset(getAssets(), "yourFont.ttf");
 btnA.setText("my custom font");
 btnA.setTypeface(typeface);
 btnA.setTextSize(20);//as per your size

and for more reference see this 有关参考,请参阅此内容

do as your second button if this helps let me know thanks... 做你的第二个按钮,如果这有助于让我知道谢谢...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在一个文本视图中为两个字符串保留两种不同的字体大小 - How to keep two different font sizes for two strings in one single text view Android textview文本有两种不同的大小 - Android textview text with two different sizes 当添加的文本是两种不同的字体大小时,如何在 JPanel 中开始换行 - How can I start a newline in a JPanel when the added text are two different font sizes 如何在 java 中为同一文本区域使用不同的字体类型和大小 - how can i use different font types and sizes for the same text area in java 在JavaFX中,是否可以在同一文本行中使用两种不同的字体大小? - Is there a way to have two different font sizes within the same text line in JavaFX? 如何在 android 中对不同的图像大小使用不同的图像压缩 - How to use different image compression on different imaze sizes in android 如何在Android中调整Button文本字体? - How to adjust Button text font in android? 如何在Android中为Button文本添加替代字体? - How to add alternative font to Button text in Android? 是否可以在一个微调器中有两种不同的字体大小 - Is it possible to have two different font sizes in one spinner 如何在NatTable单元格中插入两个文本,每个文本都有不同的字体? - How to insert into NatTable cell two text each with a different font?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM