2017-01-12 4 views
1

私は何かしようとしているあなたの助けが必要です、私は丸い角でボタンを作って、それの境界線を表示しようとしていました。私はWebサービスから何を得るかによってプログラムで色を変えていましたが、今まで私はdrawableを使ってシェイプを追加しようとしましたが、ボーダーカラーで丸みを帯びたシェイプを与えましたが、描画可能丸みを帯びた角と色のついたボタン

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

<stroke android:width="3dp" 
    android:color="#ff000000" 
    /> 

<padding android:left="1dp" 
    android:top="1dp" 
    android:right="1dp" 
    android:bottom="1dp" 
    /> 

<corners android:bottomRightRadius="7dp" 
    android:bottomLeftRadius="7dp" 
    android:topLeftRadius="7dp" 
    android:topRightRadius="7dp"/> 
にデフォルトで追加

、その後、私は私が使用した描画可能なボタンのカスタムクラスを作成し、onDrawメソッドを変更し、イム形状を得る図形を追加しようとしましたが、ちょっと奇妙です

Rounded shape

奇妙な形状のイムは、設定されたテキストの方法でプログラムでテキストを追加し、そのが表示されていないほか
@Override 
protected void onDraw(Canvas canvas) { 
    // TODO Auto-generated method stub 

    Paint paint = new Paint(); 
    paint.setStyle(Paint.Style.STROKE); 
    paint.setColor(strokeColor); 
    paint.setStrokeWidth(5.0f); 

    int h = this.getHeight(); 
    int w = this.getWidth(); 
    //final RectF rect = new RectF(); 

    RectF oval1 = new RectF(0, 0, w, h); 
    canvas.drawRoundRect(oval1, 40, 40, paint); 

} 

といくつかの理由で、それはストロークの色を取得ではなく、テキスト

buttonCTA = ButterKnife.findById(this, R.id.btnCTA); 
     buttonCTA.setTextColor(Color.parseColor(valueColor)); 
     buttonCTA.setStrokeColor(valueColor); 
     buttonCTA.setText("test"); 

答えて

5

これは必要なものです。

public static void setRoundedDrawable(Context context, View view, int backgroundColor, int borderColor) { 
    GradientDrawable shape = new GradientDrawable(); 
    shape.setShape(GradientDrawable.RECTANGLE); 
    shape.setCornerRadius(20f); 
    shape.setColor(backgroundColor); 
    if (borderColor != 0){ 
     shape.setStroke(2f, borderColor); 
    } 
    view.setBackgroundDrawable(shape); 
} 

コーナー半径とストローク幅を変更するには、必要なものによって異なります。 お手伝い願います!

+0

ありがとうございました!思ったよりも簡単だった –

0

ビューや修正を得るためにGradientDrawableを使用します。

GradientDrawable drawable = (GradientDrawable)buttonCTA.getBackground(); 
drawable.setStroke(3, Color.your_color); 

ここ3は、事前定義されたストローク幅とColor.your_colorWebServiceから来る色です。

3

round_background.xmlを描画可能フォルダに作成します。任意のビューで、実行時にバックグラウンド

<Button 
     android:id="@+id/mybutton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/round_background" 
     android:text="Hello World!" /> 

変更、それをとして設定

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <stroke 
     android:width="3dp" 
     android:color="#ff000000" /> 

    <padding 
     android:bottom="1dp" 
     android:left="1dp" 
     android:right="1dp" 
     android:top="1dp" /> 

    <corners 
     android:bottomLeftRadius="7dp" 
     android:bottomRightRadius="7dp" 
     android:topLeftRadius="7dp" 
     android:topRightRadius="7dp" /> 
</shape> 

Button button = (Button) findViewById(R.id.mybutton); 
GradientDrawable drawable = (GradientDrawable)button.getBackground(); 
drawable.setStroke(2, Color.YELLOW); 
関連する問題