[日本語]
I will introduce how to set gradation on a background color of View and round corners of View and on Android.

Environment:Android Studio 2.2.1, API 19
Use GradientDrawable for setting color gradation and rounding corners.
Set gradient colors to GradientDrawable's argument and declare gradation direction using Orientation.
GradientDrawable gradient = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[]{Color.BLUE, Color.WHITE});
Use setCornerRadius for rounding corners of View.
Use setCornerRadius for rounding corners of View.
private Button buttonSample = new Button(this);
buttonSample.setBackground(gradient);
[Sample Code]
package net.studioks.sample1;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.widget.Button;
import android.widget.RelativeLayout;
public class sample1 extends Activity {
private Button buttonSample;
@Override
public void onCreate(Bundle bundle){
super.onCreate(bundle);
RelativeLayout relativeLayout = new RelativeLayout(this);
setContentView(relativeLayout);
buttonSample = new Button(this);
buttonSample.setText("Sample");
buttonSample.setTextColor(Color.WHITE);
buttonSample.setPadding(0,0,0,0);
GradientDrawable gradient = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[]{Color.BLUE, Color.WHITE});
gradient.setCornerRadius(5);
buttonSample.setBackground(gradient);
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(100, 50);
param.leftMargin = 20;
param.topMargin = 10;
param.addRule(RelativeLayout.ALIGN_TOP);
relativeLayout.addView(buttonSample,param);
}
}
Write a comment