I asked another questıon and after, I continued to this problem...
Firstly My first Question: how to Custom Button (has two TextFields) on Android
I extended a class form LinearLayout, and I add two buttons in it(width- fill_parent, weight-1). But they can't place right. If I use LinearLayout instead开发者_StackOverflow社区of my customClass, it is working right. What Should I do??
This is my class
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);
}
}
}
The buttons you added to your SplitButtonController are XButton2, and in your constructor, you are inflating R.layout.xbutton2. This will cause an empty "XButton2" added to your SplitButtonController layout. You don't need to inflate anything if you want to create (or extend) a simple LinearLayout. Then your SplitButtonController code should look like following:
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);
}
}
}
精彩评论