Android:如何在RelativeLayout的子类中初始化组件

时间:2016-02-16 07:07:29

标签: android android-layout android-fragments

到目前为止我做了什么

我是Android新手。我正在尝试写一个音乐理论应用程序。我想写一个RelativeLayout的子类,其中包含一组ToggleButtons。下面的代码生成了这个截图(选中了四个切换按钮,我通过在UI中按下它们来实现;我没有以编程方式选择它们):

Screenshot of my pitch selector fragment

import android.content.Context;
import android.util.AttributeSet;
import android.widget.RelativeLayout;
import android.widget.ToggleButton;


public class PitchSelectorView extends RelativeLayout {

    public static final String[] PITCH_NAMES = {"C", "C#", "D", "D#", "E", "F",
                                                "F#", "G", "G#", "A", "A#", "B"};
    public static final int BUTTON_WIDTH = 100;
    public static final int BUTTON_HEIGHT = 100;
    public static final int CIRCLE_SIZE = 250;
    private ToggleButton[] buttons = new ToggleButton[PITCH_NAMES.length];

    public PitchSelectorView(Context context) {
        super(context);
        init(context);
    }

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

    public PitchSelectorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    private void init(Context context) {
        double delta = Math.PI * 2 / buttons.length;
        double angle = -90.0;
        double x;
        double y;

        for (int i = 0; i < buttons.length; i++) {
            angle += delta;
            x = (Math.cos(angle) * CIRCLE_SIZE) + CIRCLE_SIZE;
            y = (Math.sin(angle) * CIRCLE_SIZE) + CIRCLE_SIZE;

            ToggleButton button = new ToggleButton(context);
            button.setBackgroundResource(R.drawable.pitch_button);
            button.setMinimumWidth(BUTTON_WIDTH * (int) getResources().getDisplayMetrics().density);
            button.setMinimumHeight(BUTTON_HEIGHT * (int) getResources().getDisplayMetrics().density);
            button.setWidth(BUTTON_WIDTH * (int) getResources().getDisplayMetrics().density);
            button.setHeight(BUTTON_HEIGHT * (int) getResources().getDisplayMetrics().density);
            String text = Integer.toString(i) + " " + PITCH_NAMES[i];
            button.setText(text);
            button.setTextOn(text);
            button.setTextOff(text);

            RelativeLayout.LayoutParams layout = (RelativeLayout.LayoutParams) button.getLayoutParams();
            if (layout == null) {
                layout = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT);
            }
            layout.leftMargin = (int) x;
            layout.topMargin = (int) y;

            buttons[i] = button;

            addView(button, layout);
        }
    }
}

如何正确地做到这一点?

我很确定我没有正确地做到这一点。我希望将变量BUTTON_WIDTHBUTTON_HEIGHTCIRCLE_SIZE基于用户的屏幕大小,但我在init(context)方法中无法使用父片段的大小。< / p>

我不确定我应该在哪里初始化ToggleButtons并将其添加到RelativeLayout

1 个答案:

答案 0 :(得分:0)

所以我发现自己错了。我需要做的是将ViewGroup子类化以定义子元素的排列方式,然后创建子元素并将它们添加到我的Activity中的自定义ViewGroup(或者我猜片段)。本教程帮助我理解:https://dzone.com/articles/how-to-create-a-custom-layout-in-android-by-extend