【问题标题】:Format text within a button格式化按钮内的文本
【发布时间】:2012-07-19 11:27:19
【问题描述】:

我正在开发一个自定义数字选择器,我有一个带有“+”和“-”符号的按钮。
目前我只有这段代码来创建按钮。

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

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


我怎样才能做到这一点?
所以我需要将最大标签居中,并给它一个不同的大小作为加号。

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

【问题讨论】:

标签: android button text-formatting


【解决方案1】:

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

但是在你的布局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

【讨论】:

    【解决方案2】:

    许多解决方案:

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

    希望对你有帮助

    【讨论】:

      【解决方案3】:

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

      这是布局:

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

      您需要一个可绘制的形状作为背景(以显示点击): 你不会有颜色,只是用你的颜色替换它们。

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

      【讨论】:

        【解决方案4】:
        increment = new Button(context);
        increment.setTextSize(25);
        String text="<h4>Max:10</h4><h1><b>+</b></h1>";
        increment.setText(Html.formHtml(text));
        

        【讨论】:

          猜你喜欢
          • 2017-03-08
          • 1970-01-01
          • 1970-01-01
          • 2021-12-15
          • 2013-08-13
          • 1970-01-01
          • 2017-06-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多