我问了另一个问题,之后,我继续这个问题......
首先我的第一个问题:如何在 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);
}
}
}