2013-10-31 80 views
0

按钮的文字我已经在我的应用程序的按钮中,我使用的设置它的背景: 的setBackground(可绘制背景)设置圆背景Android中

然后我使用的setText(CharSequence的,TextView的。 BufferType)在底部显示文本。

现在的问题是我想把文本上的圆角背景。我正在查看按钮的android文档,但无法找到任何有用的函数来为按钮的文本添加背景。

更清楚: Image http://i41.tinypic.com/1hc9og.png

我想获得黄色部分。任何帮助/建议将不胜感激。谢谢!

编辑:要清楚红色的形状是按钮本身。而黄色的形状是我想要的按钮文本周围的背景。

回答

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

希望我帮了忙。

+0

感谢您的输入。我试过你的解决方案,它填满了整个按钮。我只想要一个按钮文本周围的形状/背景。和按钮本身的不同背景。 – Manu

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

我知道这是旧的文章,但我认为它可以帮助很多在这里。不要使用drawable,而是尝试扩展按钮并在画布上绘制椭圆。

这里所说:

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