9パッチまたはxmlを使用してマルチカラーボーダーEditTextを作成できます<layer-list>
。ここでは、マルチカラーボーダーEditTextをプログラムで作成しています。
public class MainActivity extends AppCompatActivity {
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
customTextViewWithBorder();
}
private void customTextViewWithBorder(){
mTextView = (TextView) findViewById(R.id.tv);
// Initialize some new ColorDrawable objects
ColorDrawable leftBorder = new ColorDrawable(Color.RED);
ColorDrawable topBorder = new ColorDrawable(Color.GREEN);
ColorDrawable rightBorder = new ColorDrawable(Color.BLUE);
ColorDrawable bottomBorder = new ColorDrawable(Color.YELLOW);
ColorDrawable background = new ColorDrawable(Color.WHITE);
// Initialize an array of Drawable objects
Drawable[] layers = new Drawable[]{
leftBorder, // Red color
topBorder, // Green color
rightBorder, // Blue color
bottomBorder, // Yellow color
background // White background
};
// Initialize a new LayerDrawable
LayerDrawable layerDrawable = new LayerDrawable(layers);
// Red layer padding, draw left border
layerDrawable.setLayerInset(0,0,0,15,0);
// Green layer padding, draw top border
layerDrawable.setLayerInset(1,15,0,0,15);
// Blue layer padding, draw right border
layerDrawable.setLayerInset(2,15,15,0,0);
// Yellow layer padding, draw bottom border
layerDrawable.setLayerInset(3,15,15,15,0);
// White layer, draw the background
layerDrawable.setLayerInset(4,15,15,15,15);
mTextView.setBackground(layerDrawable);
// Set the TextView padding
mTextView.setPadding(25,25,25,25);
}
ソースandroid--コード