3

カスタムナンバーピッカーに取り組んでおり、「+」と「-」の記号が付いたボタンがあります。
現時点では、ボタンを作成するためのこのコードがあります。

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

ここで、このようなボタンに表示される最小値と最大値を追加したいと思います。

ターゲットルックの例
どうすればこれを達成できますか?
したがって、最大ラベルを中央に配置し、プラス記号とは異なるサイズを指定する必要があります。

どんな助けでも大歓迎です!:)

4

4 に答える 4

2

これがあなたの必要なものかどうかはわかりません、、

しかし、レイアウト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
于 2012-07-19T11:38:36.223 に答える
1

多くの解決策:

  • 標準のボタンを使用して、その上に多くの行を書き込むことができます(「\ n」が付いている場合があります)
  • Buttonクラスを拡張するカスタムボタンを作成し、onDrawメソッドをオーバーライドして最大値と最小値を書き込むことができます。
  • 3つのtextView(または2つのtextViewとimageView)を含むレイアウトを作成し、クリック効果のbackgroundDrawableを追加できます。

これがお役に立てば幸いです

于 2012-07-19T11:41:39.987 に答える
1

独自のカスタムボタンを作成できます。

レイアウトは次のとおりです。

<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);
于 2012-07-19T11:45:35.813 に答える
0
increment = new Button(context);
increment.setTextSize(25);
String text="<h4>Max:10</h4><h1><b>+</b></h1>";
increment.setText(Html.formHtml(text));
于 2012-07-19T11:38:57.937 に答える