2012-10-23 4 views
3

텍스트 옆에 맞춤 검색 버튼을 만들려고합니다. 나는 그들 옆에 (x)가있는 항목 목록을 가지므로 사용자는 (x)를 눌러 항목을 삭제할 수 있습니다. 이처럼 ...옆에 텍스트가있는 Android 맞춤 버튼

(x)의 항목 1 (x)의 항목 2 (X) 항목 3

I 버튼을 확장하는 클래스를 가지고,하지만 난이 때 때문에, 오버라이드 (override)되어있는 무슨 방법을 잘 모르겠어요 사용자 정의 클래스를 사용하여 단추를 작성하면 일반 단추로 표시됩니다. 여기 내 코드가있다. 당신은있는 LinearLayout를 확장하는 사용자 정의보기를 만들 수

답변

1

당신의 attrs PARAMS와 방법 및 생성자를 onDraw는 오버라이드 (override) 할 필요가

public LabelButton(Context context, AttributeSet attrs, int defStyle) { 
      super(context, attrs, defStyle); 
       if (attrs != null) { 
       // set your attrs 
     } 
     } 

@Override 
protected synchronized void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    Paint textPaint = new Paint(); 
    textPaint.setAntiAlias(true); 
    textPaint.setColor(textColor); 
    textPaint.setTextSize(textSize); 
    Rect bounds = new Rect();  
    textPaint.getTextBounds(totalText, 0, totalText.length(), bounds); 
    int x = getWidth()/2 - bounds.centerX(); 
    int y = getHeight()/2 - bounds.centerY(); 
    canvas.drawText(text, getLeft(), y, textPaint);// draw your text in coords 
} 

public synchronized void setText(String text) { 
    if (text != null) { 
     this.text = text; 
    } else { 
     this.text = ""; 
    } 
    postInvalidate(); 
} 
1

public class LabelButton extends Button { 
    private final Context context; 

    private final String label; 

    public LabelButton(Context context, String label) { 
    super(context); 
    this.context = context; 
    this.label = label; 
    } 

    public View getView(View convertView, ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View labelView = inflater.inflate(R.layout.label, parent, false); 
    TextView textView = (TextView) labelView.findViewById(R.id.label); 
    Button button = (Button) labelView.findViewById(R.id.buttonCancel); 
    textView.setText("X"); 
    return labelView; 
    } 

는}, 다음 버튼과 텍스트 뷰를 포함하는 수평의 LinearLayout으로 XML을 팽창. 맞춤 설정을 위해 this tutorial과 같은 스타일 속성을 만드는 것이 좋습니다.

관련 문제