2

我问了另一个问题,之后,我继续这个问题......

首先我的第一个问题:如何在 Android 上自定义按钮(有两个 TextFields)

我扩展了一个类表单 LinearLayout,并在其中添加了两个按钮(width-fill_parent,weight-1)。但他们不能正确放置。如果我使用 LinearLayout 而不是我的 customClass,它工作正常。我该怎么办??

这是我的课

public class SplitButtonController extends LinearLayout
    implements
        OnClickListener {

// Toggle buttons
private Vector<XButton2> buttons;

// Listener
private OnClickListener listener;

public SplitButtonController(Context context, AttributeSet attrs) {
    super(context, attrs);
    LayoutInflater layoutInflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = layoutInflater.inflate(R.layout.xbutton2, this);

}

public SplitButtonController(Context context) {
    super(context);
}

@Override
protected void onFinishInflate() {
    super.onFinishInflate();
    init();
}

/**
 * Initialize the toggle buttons (set images and listeners). It's
 * responsibility of the user call this method after he add a ne
 */
public void init() {
    buttons = new Vector<XButton2>();
    addLayoutButtons();
    changeButtonsImage();
    setListeners();
}
private void addLayoutButtons() {
    int n = getChildCount();
    for (int i = 0; i < n; i++) {
        View v = getChildAt(i);
        if (v instanceof XButton2) {
            buttons.add((XButton2) v);
        }
    }
}

private void changeButtonsImage() {
    if (buttons.size() > 1) {
        buttons.get(0)
                .setBackgroundResource(
                        com.matriksdata.bavul.R.drawable.schedule_left_button_drawable);

        for (int i = 1; i < buttons.size() - 1; i++) {
            // buttons.get(i).setBackgroundResource(R.drawable.schedule_left_button_drawable);
        }
        buttons.get(buttons.size() - 1)
                .setBackgroundResource(
                        com.matriksdata.bavul.R.drawable.schedule_right_button_drawable);
    } else {
        // TODO:set an image with rounded sides
    }
}

private void setListeners() {
    for (int i = 0; i < buttons.size(); i++) {
        buttons.get(i).setOnClickListener(this);
        buttons.get(i).setFocusable(true);
    }
}

@Override
public void onClick(View v) {
    for (int i = 0; i < buttons.size(); i++) {
        XButton2 b = buttons.get(i);
        b.setChecked(v == b);
    }

}

}
4

1 回答 1

2

您添加到 SplitButtonController 的按钮是 XButton2,在您的构造函数中,您正在膨胀 R.layout.xbutton2。这将导致一个空的“XButton2”添加到您的 SplitButtonController 布局中。如果你想创建(或扩展)一个简单的 LinearLayout,你不需要膨胀任何东西。然后您的 SplitButtonController 代码应如下所示:

public class SplitButtonController extends LinearLayout
    implements
        OnClickListener {

// Toggle buttons
private Vector<XButton2> buttons;

// Listener
private OnClickListener listener;

public SplitButtonController(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void onFinishInflate() {
    super.onFinishInflate();
    init();
}

/**
 * Initialize the toggle buttons (set images and listeners). It's
 * responsibility of the user call this method after he add a ne
 */
public void init() {
    buttons = new Vector<XButton2>();
    addLayoutButtons();
    changeButtonsImage();
    setListeners();
}
private void addLayoutButtons() {
    int n = getChildCount();
    for (int i = 0; i < n; i++) {
        View v = getChildAt(i);
        if (v instanceof XButton2) {
            buttons.add((XButton2) v);
        }
    }
}

private void changeButtonsImage() {
    if (buttons.size() > 1) {
        buttons.get(0)
                .setBackgroundResource(
                        com.matriksdata.bavul.R.drawable.schedule_left_button_drawable);

        for (int i = 1; i < buttons.size() - 1; i++) {
            // buttons.get(i).setBackgroundResource(R.drawable.schedule_left_button_drawable);
        }
        buttons.get(buttons.size() - 1)
                .setBackgroundResource(
                        com.matriksdata.bavul.R.drawable.schedule_right_button_drawable);
    } else {
        // TODO:set an image with rounded sides
    }
}

private void setListeners() {
    for (int i = 0; i < buttons.size(); i++) {
        buttons.get(i).setOnClickListener(this);
        buttons.get(i).setFocusable(true);
    }
}

@Override
public void onClick(View v) {
    for (int i = 0; i < buttons.size(); i++) {
        XButton2 b = buttons.get(i);
        b.setChecked(v == b);
    }

}

}
于 2011-09-23T10:35:18.793 回答