0

我希望在整个应用程序中都有一个滑动菜单,我的应用程序中的每个活动都可以访问该菜单。为此,我创建了一个父活动,所有其他活动都按照此答案中的建议扩展到该活动. 因此,只有一个活动将实现该滑动菜单功能,而扩展该活动的所有其他活动都将具有该实现。但我的问题是,当我按下 UP 按钮(应用程序图标)时,滑动抽屉应该显示出来,整个布局应该移到右侧。这适用于 BaseActivity,但对于扩展该类的活动,它不起作用。既没有显示抽屉,也没有向右移动布局。我尝试将 toast 放入 BaseActivity 的适当方法中,显示 Toast 但抽屉未显示。可能是什么问题?

布局xml如下,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_root"
android:layout_width="wrap_content"
android:layout_height="fill_parent" >

<RelativeLayout
    android:id="@+id/ll_lhs_menu"
    android:layout_width="300dip"
    android:layout_height="fill_parent"
    android:background="@color/menu_bg"
    android:orientation="vertical"
    android:visibility="visible" >

    .......
    Sliding drawer goes here.
    .......

</RelativeLayout>


<!-- Following is the actual layout where in the layout for other activities
     will be put in using the java code below. -->
<RelativeLayout
    android:id="@+id/rl_right"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentRight="true"
    android:background="@drawable/capture_port"
    android:scrollbars="none" >

</RelativeLayout>

</RelativeLayout>

重写的setContentView()方法如下:

    @Override
public void setContentView(int layoutResID) {
    _completeLayout= (RelativeLayout) getLayoutInflater().inflate(R.layout.menu, null);
    _activityLayout= (RelativeLayout) _completeLayout.findViewById(R.id.rl_right);
    getLayoutInflater().inflate(layoutResID, _activityLayout, true);
    setContentView(_completeLayout);
}

单击操作栏上的 UP 按钮时,将执行以下代码BaseActivity

    @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
    _leftLayout.setVisibility(View.VISIBLE);
    int width = _leftLayout.getWidth();
    if (width == 0) {
        width = 300;
    }

    if (!isMenuVisible()) {
        Toast.makeText(this, "Executed successfully...", Toast.LENGTH_LONG).show();
        _leftLayout.setVisibility(View.VISIBLE);

        _rightLayoutParams = new LayoutParams(
            _rightLayout.getWidth(), _rightLayout.getHeight());
        _rightLayoutParams.setMargins(width, 0, -width, 0);
        _rightLayout.setLayoutParams(_rightLayoutParams);

        Animation slideRight = setRightSlidingAnimation();
        _leftLayout.startAnimation(slideRight);
        _rightLayout.startAnimation(slideRight);
    } else {
        Animation slideLeft = setLeftSlidingAnimation();
        _rightLayout.startAnimation(slideLeft);
        _leftLayout.startAnimation(slideLeft);
    }

    setMenuVisible(!isMenuVisible());
    break;
    }
    return false;
}
4

1 回答 1

2

onOptionsItemSelected问题解决了,因为现在正确的布局已经改变,所以只需要再次初始化视图。该方法现在看起来像:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:

    _rootLayout = (RelativeLayout) findViewById(R.id.rl_root);
    _leftLayout = (RelativeLayout) findViewById(R.id.ll_lhs_menu);
    _rightLayout = (RelativeLayout) findViewById(R.id.rl_right);

    _leftLayout.setVisibility(View.VISIBLE);
    int width = _leftLayout.getWidth();
    if (width == 0) {
        width = 300;
    }

if (!isMenuVisible()) {
    Toast.makeText(this, "Executed successfully...", Toast.LENGTH_LONG).show();
    _leftLayout.setVisibility(View.VISIBLE);

    _rightLayoutParams = new LayoutParams(
        _rightLayout.getWidth(), _rightLayout.getHeight());
    _rightLayoutParams.setMargins(width, 0, -width, 0);
    _rightLayout.setLayoutParams(_rightLayoutParams);

    Animation slideRight = setRightSlidingAnimation();
    _leftLayout.startAnimation(slideRight);
    _rightLayout.startAnimation(slideRight);
} else {
    Animation slideLeft = setLeftSlidingAnimation();
    _rightLayout.startAnimation(slideLeft);
    _leftLayout.startAnimation(slideLeft);
}

setMenuVisible(!isMenuVisible());
break;
}
return false;
}
于 2012-07-17T13:27:50.413 回答