父活动的code得到执行,但结果(动画)没有显示出来 [英] Parent Activity's code getting executed, but the result (animation) is not showing up

查看:116
本文介绍了父活动的code得到执行,但结果(动画)没有显示出来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有我的整个应用程序中的滑动菜单,这将是在我的应用程序的每个活动进行访问。对于这一点,我创建了一个父活动,而所有其他活动扩展为在这个答案建议。这样,只有一个活动将实现滑动菜单功能和扩展该活动的所有其他活动将有落实。但我的问题是,当我preSS按钮(应用程序图标)的滑动抽屉应该出现了,整个布局应该转移到右侧。这工作正常的BaseActivity,但对于活动的扩展这个类,它不工作。无论抽屉越来越显示,也没有布局转向右侧。我试图把在BaseActivity的适当的方法烤面包,面包越来越显示,但是抽屉没有显示出来。可能是什么问题?

布局XML如下,

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@ + ID / rl_root
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =FILL_PARENT>

< RelativeLayout的
    机器人:ID =@ + ID / ll_lhs_menu
    机器人:layout_width =300dip
    机器人:layout_height =FILL_PARENT
    机器人:背景=@色/ menu_bg
    机器人:方向=垂直
    机器人:能见度=有形与GT;

    .......
    滑动抽屉放在这里。
    .......

< / RelativeLayout的>


<! - 下面是实际的布局,其中在从事其他活动的布局
     将投入使用下面的Java code。 - >
< RelativeLayout的
    机器人:ID =@ + ID / rl_right
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:layout_alignParentRight =真
    机器人:背景=@可绘制/ capture_port
    机器人:滚动条=无>

< / RelativeLayout的>

< / RelativeLayout的>
 

和重写的的setContentView()方法如下:

  @覆盖
公共无效的setContentView(INT layoutResID){
    _completeLayout =(RelativeLayout的)getLayoutInflater()膨胀(R.layout.menu,空)。
    _activityLayout =(RelativeLayout的)_completeLayout.findViewById(R.id.rl_right);
    。getLayoutInflater()膨胀(layoutResID,_activityLayout,真正的);
    的setContentView(_completeLayout);
}
 

在点击操作栏上按钮的,下面code在 BaseActivity

执行

  @覆盖
公共布尔onOptionsItemSelected(菜单项项){
    开关(item.getItemId()){
    案例android.R.id.home:
    _leftLayout.setVisibility(View.VISIBLE);
    INT宽度= _leftLayout.getWidth();
    如果(宽== 0){
        宽= 300;
    }

    如果(!isMenuVisible()){
        Toast.makeText(这一点,成功运行了......,Toast.LENGTH_LONG).show();
        _leftLayout.setVisibility(View.VISIBLE);

        _rightLayoutParams =新的LayoutParams(
            _rightLayout.getWidth(),_rightLayout.getHeight());
        _rightLayoutParams.setMargins(宽度,0, - 宽度,0);
        _rightLayout.setLayoutParams(_rightLayoutParams);

        动画slideRight = setRightSlidingAnimation();
        _leftLayout.startAnimation(slideRight);
        _rightLayout.startAnimation(slideRight);
    } 其他 {
        动画slideLeft = setLeftSlidingAnimation();
        _rightLayout.startAnimation(slideLeft);
        _leftLayout.startAnimation(slideLeft);
    }

    setMenuVisible(isMenuVisible()!);
    打破;
    }
    返回false;
}
 

解决方案

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

  @覆盖
公共布尔onOptionsItemSelected(菜单项项){
    开关(item.getItemId()){
    案例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宽度= _leftLayout.getWidth();
    如果(宽== 0){
        宽= 300;
    }

如果(!isMenuVisible()){
    Toast.makeText(这一点,成功运行了......,Toast.LENGTH_LONG).show();
    _leftLayout.setVisibility(View.VISIBLE);

    _rightLayoutParams =新的LayoutParams(
        _rightLayout.getWidth(),_rightLayout.getHeight());
    _rightLayoutParams.setMargins(宽度,0, - 宽度,0);
    _rightLayout.setLayoutParams(_rightLayoutParams);

    动画slideRight = setRightSlidingAnimation();
    _leftLayout.startAnimation(slideRight);
    _rightLayout.startAnimation(slideRight);
} 其他 {
    动画slideLeft = setLeftSlidingAnimation();
    _rightLayout.startAnimation(slideLeft);
    _leftLayout.startAnimation(slideLeft);
}

setMenuVisible(isMenuVisible()!);
打破;
}
返回false;
}
 

I want to have a sliding menu throughout my application which will be accessible for every activity in my application. For this, I created a parent activity to which all other activities extend as suggested in this answer. So that, only one activity will implement that sliding menu functionality and all other activities extending that activity will have that implementation. But my problem is, when I press UP button (app icon) the sliding drawer should show up and the whole layout should shift to the right side. This works fine for the BaseActivity, but for the activities extending that class, its not working. Neither the drawer is getting shown, nor the layout is shifting towards right. I tried putting toasts in the BaseActivity's appropriate method, the Toast is getting shown but the drawer is not showing up. What can be the problem?

The layout xml is as follows,

<?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>

And the overridden setContentView() method is as follows:

    @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);
}

On click of UP button on action bar, following code is executed in 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;
}

解决方案

Problem solved, just had to initialize the view once again in onOptionsItemSelected since the right layout has changed now. The method now looks like:

@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;
}

这篇关于父活动的code得到执行,但结果(动画)没有显示出来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆