2016-08-30 4 views
0

How to do this in android ? Is it possible using some selector designs? I have one option that I can use a icon for this and set it as drawableLeft but without using the icon how can I achieve this please guide me in good direction모서리 반지름과 시작 색으로 안드로이드 편집 스타일을 만드는 방법은 무엇입니까?

안드로이드에서이 작업을 수행하는 방법은 무엇입니까? 일부 셀렉터 디자인을 사용할 수 있습니까? 나는이 좋은 방향으로 나를 인도 해주십시오의 아이콘을 사용하고 난이 달성 할 수있는 방법 아이콘을 사용하여 drawableLeft으로 만하지 않고 설정할 수 있습니다 하나 개의 옵션

답변

1

사용자 정의 Drawable으로이를 수행 할 수 있습니다. 뭔가 같은 : 다음

public class BackgroundColorDrawable extends Drawable { 

    private Paint paint; 
    private RectF rectF; 
    private float cornerRadius = 20f; 
    private float borderThickness = 3.5f; 
    private int insetColour = Color.GREEN; 

    public BackgroundColorDrawable() { 

     paint = new Paint(); 
     paint.setAntiAlias(true); 
     rectF = new RectF(); 
    } 

    public void setInsetColour(int insetColour) { 
     this.insetColour = insetColour; 
     invalidateSelf(); 
    } 

    public void setBorderThickness(float borderThickness) { 
     this.borderThickness = borderThickness; 
     invalidateSelf(); 
    } 

    public void setCornerRadius(float cornerRadius) { 
     this.cornerRadius = cornerRadius; 
     invalidateSelf(); 
    } 

    @Override 
    public void draw(Canvas canvas) { 

     paint.setColor(Color.GRAY); 
     rectF.set(0f, 0f, canvas.getWidth(), canvas.getHeight()); 
     canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, paint); 

     paint.setColor(Color.WHITE); 
     rectF.set(borderThickness, borderThickness, canvas.getWidth() - borderThickness, canvas.getHeight() - borderThickness); 
     canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, paint); 

     paint.setColor(insetColour); 
     rectF.set(borderThickness, borderThickness, 60f, canvas.getHeight() - borderThickness); 
     canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, paint); 

     paint.setColor(Color.WHITE); 
     rectF.set(30f, borderThickness, 60f, canvas.getHeight() - borderThickness); 
     canvas.drawRect(rectF, paint); 
    } 

    @Override 
    public void setAlpha(int alpha) { /* to implement */ } 

    @Override 
    public void setColorFilter(ColorFilter colorFilter) { /* to implement */} 

    @Override 
    public int getOpacity() { 
     return PixelFormat.TRANSLUCENT; 
    } 
} 

당신이 당신의 EditText를 사용하는 곳이 바로 코드의 배경 당김 설정해야합니다 :

BackgroundColorDrawable drawable = new BackgroundColorDrawable(); 
editText.setBackground(drawable); 

참고이 예제의 오버 드로 꽤 거기에 (같은 픽셀이 도착를 몇 번 그려서) 최적화 할 수 있습니다.

EditText에서 왼쪽 패딩을 설정해야 커서가 올바르게 정렬됩니다.

+0

답변을 주셔서 감사합니다 정말 아주 좋은 솔루션이며 정확히 내가 무엇을 찾고 있습니다. @ 야영드 – Krrish

0

당신은 EditText
editText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.pic_name, 0);

background-image을 사용할 수 있습니다
+0

그래, 나는 그것을 알고 있었지만 이미지를 사용하면 일부 선택자를 사용하는 것과 같은 방법이있다. @ CodeWalker – Krrish

관련 문제