设置按钮内的文本格式

问题描述:

我正在使用自定义的数字选择器,并且我有一个带有'+'和' - '符号的按钮。
目前我只有这个代码来创建按钮。设置按钮内的文本格式

increment = new Button(context); 
increment.setTextSize(25); 
increment.setText("+"); 

现在,我想添加这样的按钮上显示的最小值和最大值。

example of target look
我该如何做到这一点?
所以我需要居中最大标签,并给它一个不同的大小作为加号。

任何帮助将不胜感激! :)

+0

您是否需要以编程方式执行此操作? – Carnal 2012-07-19 11:31:35

+0

http://*.com/questions/2159847/is-there-any-example-about-spanned-and-spannable-text可能有所帮助,或者只是使用“+”作为图像里面的复合可绘制 – logcat 2012-07-19 11:36:24

我不知道这是否是你的需要或不,,

但在布局xml文件,

<Button 
     android:id="@+id/btn1" 
     android:layout_width="0dip" 
     android:background="@android:color/darker_gray" 
     android:drawableBottom="@drawable/image_plus" 
     android:text="Max: 10" 
     /> 

<!-- 
android:drawableBottom="@drawable/image_plus" // plus sign from drawable 
android:text="Max: 10" // Text you want to put on top of image 
--> 

或者使用代码,

increment = new Button(context); 
increment.setTextSize(25); 
increment.setText("Max: 10"); 
increment.setCompoundDrawables(null, null, null, getResources().getDrawable(R.drawable.plus_sign)); // Here you have to apply + sign image to drawable 
+0

Thx,这做了工作:) – 2012-07-19 14:16:28

increment = new Button(context); 
increment.setTextSize(25); 
String text="<h4>Max:10</h4><h1><b>+</b></h1>"; 
increment.setText(Html.formHtml(text)); 

许多解决方案:

  • 您可以使用标准按钮并在其上写很多行(带“\ n”)
  • 您可以创建一个自定义按钮来扩展Button类,并覆盖onDraw方法来编写最大值和最小值。
  • 您可以创建一个包含3个textViews(或2个textViews和一个imageView)的布局,并为click效果添加一个backgroundDrawable。

希望这将帮助你

您可以创建自己的自定义按钮:

这是布局:

<com.your.package.ui.widget.IncrementButton xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:background="@drawable/button_grey" > 

    <TextView 
     android:id="@+id/increment_label" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:text="+" 
     android:textColor="@android:color/black" 
     android:textSize="22sp" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@id/increment_label" 
     android:layout_centerHorizontal="true" 
     android:text="Max 10" 
     android:textColor="#FFFFFF" 
     android:textSize="14sp" /> 

</com.your.package.ui.widget.IncrementButton> 

你需要的形状绘制的背景(展示点击): 你不会有颜色byt只是用你的颜色替换它们。

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:state_pressed="true"><shape> 
      <padding android:bottom="10dip" android:left="10dip" android:right="10dip" android:top="10dip" /> 

      <corners android:radius="5dip" /> 

      <stroke android:width="1dip" android:color="@color/black" /> 

      <gradient android:angle="270" android:endColor="@color/button_dark_grey" android:startColor="@color/button_light_grey" /> 
     </shape></item> 
    <item android:state_focused="true"><shape> 
      <padding android:bottom="10dip" android:left="10dip" android:right="10dip" android:top="10dip" /> 

      <corners android:radius="5dip" /> 

      <stroke android:width="1dip" android:color="@color/black" /> 

      <gradient android:angle="45" android:endColor="@color/button_dark_grey" android:startColor="@color/button_light_grey" /> 
     </shape></item> 
    <item><shape> 
      <padding android:bottom="10dip" android:left="10dip" android:right="10dip" android:top="10dip" /> 

      <corners android:radius="5dip" /> 

      <stroke android:width="1dip" android:color="@color/black" /> 

      <gradient android:angle="270" android:endColor="@color/button_light_grey" android:startColor="@color/button_dark_grey" /> 
     </shape></item> 

</selector> 

然后,你还要在按钮上的完全控制:

package com.your.package.ui.widget; 



public class IncrementButton extends RelativeLayout implements OnClickListener { 

    private OnIncrementClick onIncrementClickListener; 

    public interface OnIncrementClick { 
     void onIncrementClick(); 
    } 

    public IncrementButton(Context context) { 
     super(context); 
     init(); 
    } 

    public IncrementButton(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 

    public IncrementButton(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     init(); 
    } 

    private void init() { 
     setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 
     if(this.onIncrementClickListener != null) 
      this.onIncrementClickListener.onIncrementClick(); 
    } 

    public void setOnIncrementClickListener(OnIncrementClick onIncrementClickListener) { 
     this.onIncrementClickListener = onIncrementClickListener; 
    } 
} 

然后,您可以包括在您的任何布局的按钮,引用它并添加点击监听器(就像一个正常的按钮) 。

IncrementButton button = (IncrementButton) findViewById(R.id.whatever); 
button.setOnIncrementClickListener(yourListener);