2016-04-30 94 views
2

我有一个椭圆形的背景下按钮,它看起来并不像一个完美的圆,我可以在每circunfrence距离“休息”注意到,这是它的外观安卓:椭圆形不妙

enter image description here

背景:

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

<solid android:color="#7eadb6"/> 
<size android:height="80dp" 
    android:width="80dp"/> 

编辑:创建按钮

static Button theButton1; 
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams 
      (80, 80); 

theButton1 = new Button(getActivity()); 
theButton1.setBackgroundResource(R.drawable.subject_button); 
theButton1.setLayoutParams(params); 
theButton1.setTextColor(Color.parseColor("#ffffff")); 
theButton1.setTextSize(23); 
theButton1.setText(prefs.getString("text1", "").substring(0, 1)); 
theButton1.setOnClickListener(this); 

这是正常的吗?如果不是我该如何解决它?

注:我有一个以上的按钮,这样

+0

为什么选择Java?难道你只是将它设置为xml布局? –

+0

这是什么原因造成的? –

+0

您可以改用图片。 –

回答

2

错误的问题导致错误的答案!

通过阅读您的意见,我知道你想在最后

enter image description here

上有一个文本的圆圈来实现这样的事情。

您可以轻松地通过使用TextDrawable Library

达到你的目标现在,让我们回到你的问题,这个问题是肯定只是因为抗锯齿。您的问题在l-dpi设备上变得更糟。

诀窍是制作一个位图并将其传递为所需视图的背景。

public Bitmap getCircleBitmap(int dimenSize,int mColor) { 
    Bitmap output = Bitmap.createBitmap(dimenSize, dimenSize, Bitmap.Config.ARGB_8888); 
    Canvas canvas = new Canvas(output); 
    int x = dimenSize; 
    int y = dimenSize; 
    Paint paint = new Paint(); 
    paint.setAntiAlias(true); 
    paint.setStyle(Paint.Style.FILL); 
    paint.setColor(Color.TRANSPARENT); 
    canvas.drawPaint(paint); 
    canvas.drawBitmap(output, 0.0f, 0.0f, paint); 
    paint.setColor(mColor); 
    canvas.drawCircle(x/2, y/2, dimenSize/2, paint); 
    return output; 
} 

现在你有一个夏普圈位图,可以很容易地使用它。

+0

如何设置按钮的背景到位图? –

+0

可绘制的圆=新的BitmapDrawable(getResources(),bi TMAP); 现在你有一个drawable。然后使用myButton.setBackground(circle); @Kurlicue –

+0

这两个解决方案的工作!谢谢! :d –

0

这创造了可能是一种抗混叠的问题。您必须为其禁用硬件加速。尝试这个 -

<shape android:shape="oval" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:hardwareAccelerated="false" 
> 

<solid android:color="#7eadb6"/> 
<size android:height="80dp" 
    android:width="80dp"/> 
</shape> 

UPDATE:

您也将有按钮的层类型设置为软件。

theButton1.setLayerType(View.LAYER_TYPE_SOFTWARE, null); 

设置布局参数后。

+0

它没有工作 –

+0

它提高了绘制绘制的性能 –

+0

@Kurlicue我已经更新了我的答案。 –

0

更新 只是在这样的xml文件更新Button

<Button 
    android:layout_width="80dp" 
    android:layout_height="80dp" 
    android:text="mytext" 
    android:id="@+id/button" 
    /> 

保持activity

theButton1 = new Button(getActivity()); 
theButton1.setBackgroundResource(R.drawable.subject_button); 
theButton1.setTextColor(Color.parseColor("#ffffff")); 
theButton1.setTextSize(23); 
theButton1.setText(prefs.getString("text1", "").substring(0, 1)); 
theButton1.setOnClickListener(this); 
+0

我通过点击另一个按钮编程式地添加按钮 –

+0

您是否必须以编程方式更改'layout_width'和'layout_width'或仅仅是背景 –

+0

只是背景 –

0

我不觉得有什么不对您实现。 但尝试在xml中执行此操作,因为它可能会在运行时执行所有操作时引发问题。创建一个按钮并将其背景设置为可绘制的椭圆形状

<Button 
android:layout_width="50dp" 
android:layout_height="50dp" 
android:background="@drawable/oval" 
android:gravity="center_vertical|center_horizontal" 
android:text="button text here" 
/> 
0

没有可绘制的问题。

ovelshape.xml

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

    <solid android:color="#7eadb6"/> 

在你的XML,你需要提供DP高度&宽度。并使用视图而不是按钮。我不是没有为什么,但它对我有用。

<View android:layout_width="50dp" 
    android:layout_height="50dp" 
    android:background="@drawable/ovelshape"/>