English 中文(简体)
海关 粗金 ——重度问题
原标题:Custom LinearLayout width - weight placing problem

我再次问候,此后我继续处理这一问题。

首先,我的第一个问题:

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 insteadof my customClass, it is working right. What Should I do??

这是我的班子。

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);
    }

}

}
最佳回答

你在斯普利特顿总主计长的座子是XButton2,在你的建筑商,你正在对R.layout.xbutton2进行通货膨胀。 这将导致在斯普利特·布特顿主计长布局外增加一个空洞的“XButton2”。 如果你想要创造(或扩大)一条简单的线索,那么你就不需要late。 接着,你的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);
    }

}

}
问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签