基于配置文件重新初始化布局

时间:2015-04-07 12:12:14

标签: android

我有一个菜单活动和一个设置活动。 “设置活动”从“菜单活动”启动,因此它始终直接位于堆栈跟踪中的“菜单活动”之后。

我的菜单活动有多个按钮,每个按钮用于应用程序的每个功能。可以使用“设置活动”启用和禁用所有这些功能。这些配置保存在“首选项”中。

如果某项功能被禁用,则该功能不应作为菜单屏幕中的按钮使用。

所以......问题:我需要在用户使用Settings Activity更改配置文件后重新加载整个菜单活动,否则我总是必须重新启动应用程序,直到更改生效。

那么当点击设置活动中的某个按钮时,如何从菜单活动的布局中删除/添加按钮?

/**
 * 
 */
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    this.layoutMenu = (RelativeLayout) view.findViewById(R.id.menu_layout);
    this.circularImageView = (CircularImageView) view.findViewById(R.id.menu_imageview_profile);
    this.circularImageView.setImageDrawable(Images.loadDrawableFromFile(this.getActivity(), Paths.IMAGE_PROFILE));
    this.textViewValue = (TextView) view.findViewById(R.id.menu_textview_measure_value);
    this.textViewDate = (TextView) view.findViewById(R.id.menu_textview_measure_date);
    this.textViewUnit = (TextView) view.findViewById(R.id.menu_textview_measure_unit);

    this.buttonProfile = new ImageButton(this.getActivity());
    this.buttonProfile.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
    this.buttonProfile.setImageResource(R.drawable.ic_profile);
    this.buttonProfile.setBackground(null);

    this.buttonContacts = new ImageButton(this.getActivity());
    this.buttonContacts.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
    this.buttonContacts.setImageResource(R.drawable.ic_message);
    this.buttonContacts.setBackground(null);

    this.buttonCalc = new ImageButton(this.getActivity());
    this.buttonCalc.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
    this.buttonCalc.setImageResource(R.drawable.ic_calculator);
    this.buttonCalc.setBackground(null);

    this.buttonSettings = new ImageButton(this.getActivity());
    this.buttonSettings.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
    this.buttonSettings.setImageResource(R.drawable.ic_settings);
    this.buttonSettings.setBackground(null);

    String email = this.getEmail();
    Map<String, Boolean> config = this.sessionConfigPreferences.getConfigDetails(email);

    List<View> views = new ArrayList<View>();
    views.add(this.buttonProfile);

    if(config.get(Globals.CONFIG_CONTACTS)) {
        views.add(this.buttonContacts);
    }

    if(config.get(Globals.CONFIG_CALC)) {
        views.add(this.buttonCalc);
    }

    views.add(this.buttonSettings);

    this.circleView = new CircularLayout(this.getActivity(), this, views);

    this.layoutMenu.addView(this.circleView);
}

我的CircularLayout:

public class CircularLayout extends DragableButtonsLayout {
    private List<View> menuButtons = null;
    private ImageView imageViewCenter = null;
    private ImageButton imageButtonCenter = null;
    private int radius = -1;
    private double step = -1;
    private double angle = -1;
    private static final int CENTER_ID = 111;

    public CircularLayout(Context context, DragCallback dragCallback, List<View> menuButtons) {
        super(context);

        this.dragCallback = dragCallback;
        this.menuButtons = menuButtons;
        this.radius = Sizes.getOptimalCenterRadius(context);
        this.step = (2 * Math.PI) / menuButtons.size();

        this.initView();
    }

    private void initView() {
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

        this.setLayoutParams(layoutParams);

        RelativeLayout.LayoutParams layoutParamsCenter = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, Sizes.getOptimalMenuHeight(this.getContext()));
        layoutParamsCenter.addRule(RelativeLayout.CENTER_IN_PARENT);

        RelativeLayout relativeLayoutCenter = new RelativeLayout(this.getContext());
        relativeLayoutCenter.setLayoutParams(layoutParamsCenter);

        RelativeLayout.LayoutParams layoutParamsImageViewCenter = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParamsImageViewCenter.addRule(RelativeLayout.CENTER_IN_PARENT);

        this.imageViewCenter = new ImageView(this.getContext());
        this.imageViewCenter.setLayoutParams(layoutParamsImageViewCenter);
        this.imageViewCenter.setImageDrawable(Images.loadDrawableFromFile(this.getContext(), Paths.IMAGE_BACKGROUND_POWER));
        this.imageViewCenter.setBackground(null);

        RelativeLayout.LayoutParams layoutParamsImageButtonCenter = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParamsImageButtonCenter.addRule(RelativeLayout.CENTER_IN_PARENT);

        this.imageButtonCenter = new ImageButton(this.getContext());
        this.imageButtonCenter.setId(CENTER_ID);
        this.imageButtonCenter.setLayoutParams(layoutParamsImageButtonCenter);
        this.imageButtonCenter.setImageDrawable(Images.loadDrawableFromFile(this.getContext(), Paths.IMAGE_POWER));
        this.imageButtonCenter.setBackground(null);
        this.imageButtonCenter.setOnDragListener(new DropTargetOnDragListener());

        relativeLayoutCenter.addView(this.imageViewCenter);
        relativeLayoutCenter.addView(this.imageButtonCenter);
        relativeLayoutCenter.setOnDragListener(new MenuButtonOnDragListener());

        for(View view : this.menuButtons) {
            relativeLayoutCenter.addView(this.placeView(view));
        }

        this.addView(relativeLayoutCenter);
    }

    private View placeView(View view) {
        view.measure(0, 0);

        this.imageButtonCenter.measure(0, 0);

        int x = (int)((view.getMeasuredWidth() / 2) + this.radius * Math.cos(this.angle));
        int y = (int)((view.getMeasuredHeight() / 2) + this.radius * Math.sin(this.angle));

        this.angle += this.step;

        int deltaX = view.getMeasuredWidth();
        int deltaY = view.getMeasuredHeight();
        int deltaImageX = this.imageButtonCenter.getMeasuredWidth() / 2;
        int deltaImageY = this.imageButtonCenter.getMeasuredHeight() / 2;
        int xToDraw = ((x - deltaX) - deltaImageX);
        int yToDraw = ((y - deltaY) - deltaImageY);

        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.ABOVE, CENTER_ID);
        layoutParams.addRule(RelativeLayout.RIGHT_OF, CENTER_ID);
        layoutParams.setMargins(xToDraw, 0, 0, yToDraw);

        view.setLayoutParams(layoutParams);
        view.setOnTouchListener(this);

        return view;
    }
}

1 个答案:

答案 0 :(得分:1)

您是如何构建GUI的?如果您在运行时动态构建它,只需添加或不添加任何按钮或控件即可。