0

我正在尝试在屏幕上上下移动工具栏的简单任务,但它只能工作一次。工具栏在按下按钮时移动到顶部,但即使出现“移至底部”消息,我也无法将其移回底部。见代码:

    boolean toolBarAtBottom = true;

    private void moveToolBar(){
        LinearLayout toolBar = (LinearLayout) findViewById(R.id.toolBar);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                            LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

        lp.height = toolBar.getHeight();

        if (toolBarAtBottom){ 
            lp.addRule(RelativeLayout.ALIGN_TOP, RelativeLayout.TRUE);
        }else{  
            lp.addRule(RelativeLayout.ALIGN_BOTTOM, RelativeLayout.TRUE);
            Toast.makeText(DrawActivity.this, "move to bottom", Toast.LENGTH_SHORT).show();
        }
        toolBar.setLayoutParams(lp);
        toolBarAtBottom = !toolBarAtBottom;
    }

任何想法如何让它做不止一次?

4

1 回答 1

3

您想使用RelativeLayout.ALIGN_PARENT_BOTTOM而不是RelativeLayout.ALIGN_BOTTOM. ALIGN_BOTTOM将一个视图的底部与另一个视图的底部对齐。ALIGN_PARENT_BOTTOM会将其移动到其父容器的底部。

这是文档的链接:http: //developer.android.com/reference/android/widget/RelativeLayout.html

于 2012-10-04T14:50:05.270 回答