0

アプリにボタンがあり、 setBackground(Drawable background) を使用して背景を設定しています

次に、 setText(CharSequence,TextView.BufferType) を使用して、下部にテキストを表示しています。

問題は、テキストだけに丸みを帯びた背景を付けようとしているということです。ボタンの Android ドキュメントを見ていますが、ボタンのテキストだけの背景を配置するのに役立つ機能が見つかりません。

より明確にするために: 画像

黄色の部分を取得しようとしています。ヘルプ/提案をいただければ幸いです。ありがとう!

編集:明確にするために、赤い形はボタン自体です。黄色の形は、ボタンのテキストの周りに必要な背景です。

4

3 に答える 3

0

XMLたとえば、ボタンのカスタム背景として使用して、必要に応じてスタイルを与えることができます。

round_button_file

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#6F2500"/>
<stroke android:width="2sp" android:color="#fff" />

次に、ボタンの背景をその XML ファイルとして設定します

code for button

<Button
android:layout_width="30sp"
android:layout_height="30sp"
android:background="@drawable/round_button_file"
android:gravity="center_vertical|center_horizontal"
android:text="Text"
android:textColor="#fff" />

私が助けてくれることを願っています。

于 2013-10-31T21:30:52.647 に答える
0

これが古い投稿であることは知っていますが、ここで大いに役立つと思います。ドローアブルを使用する代わりに、ボタンを拡張してキャンバスに楕円形を描画してみてください。

ここに行きます:

public class YourButton extends Button {

    //declare needed variables
    private Context context;
    private Paint ovalPaint;

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

    private void init(Context context) {
        this.context = context;
        ovalPaint = new Paint();
        ovalPaint.setAntiAlias(true);
        ovalPaint.setStyle(Paint.Style.FILL);
        ovalPaint.setColor(context.getResources().getColor(R.color.YOUR_RED_COLOR);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawOval(getOval(canvas), ovalPaint);
        super.onDraw(canvas); //important
    }

    private RectF getOval(Canvas canvas) {
        int offset = (int)(canvas.getWidth() * .05);
        return new RectF(0 + offset, 0 + offset, canvas.getWidth() - offset, canvas.getHeight() -           offset);
    }
}

xml ファイルを次のように設定します。

<YourButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Text"
    android:textSize="30sp"
    android:textColor="#fff" />
于 2014-11-05T19:33:24.683 に答える
0

これを試して:

oval_background.xml

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval" >
        <solid android:color="#FFFF00"/>

</shape>  

oval_text.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:orientation="vertical" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:paddingLeft="30dp"
    android:paddingRight="30dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:layout_margin="10dp"

    android:gravity="center_vertical|center_horizontal"
    android:background="@drawable/oval_background"
    android:text="TEXT"/>
</LinearLayout>
于 2013-10-31T22:01:57.650 に答える