父活动的代码被执行,但结果(动画)没有显示

时间:2012-07-17 12:48:12

标签: android android-layout layout-inflater

我希望在我的应用程序中有一个滑动菜单,可以访问我的应用程序中的每个活动。为此,我创建了一个父活动,所有其他活动都按照this answer中的建议进行扩展。因此,只有一个活动将实现该滑动菜单功能,并且扩展该活动的所有其他活动将具有该实现。但我的问题是,当我按下向上按钮(应用程序图标)时,滑动抽屉应该出现,整个布局应该转移到右侧。这适用于BaseActivity,但对于扩展该类的活动,它不起作用。抽屉既没有显示,也没有布局向右移动。我尝试在BaseActivity的适当方法中使用toast,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);
}

单击操作栏上的“向上”按钮,将在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;
}

1 个答案:

答案 0 :(得分: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;
}