【问题标题】:android:How to move a RelativeLayout permanently after animationandroid:如何在动画后永久移动RelativeLayout
【发布时间】:2016-06-17 14:28:12
【问题描述】:

这是activity.xml,content_my.xml是RelativeLayout,menu.xml是LinearLayout。顶部的内容布局(RelativeLayout)和下方的菜单布局(LinearLayout)我想将相对布局(内容布局)移动到屏幕左侧并访问其下方的菜单布局。动画后内容布局位于菜单布局的顶部,无法访问菜单。在 contentlayout 中,我添加了 ListView。请帮助我如何在动画后永久移动RelativeLayout。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.bwd.demo.mybrand.My">

<include layout="@layout/menu" />
<include layout="@layout/content_my" />
</android.support.design.widget.CoordinatorLayout>

【问题讨论】:

    标签: java android android-layout animation


    【解决方案1】:

    在您的 content_my.xml 中,您必须为您的 RelativeLayout 分配一个 ID:

    android:id="@+id/rl_content"
    

    然后您将 xml 转换为 Activity 中的 Java 代码,以便您可以使用它:

    RelativeLayout rl_content = (RelativeLayout) findViewById(R.id.rl_content); 
    

    现在有多个选项可以对其进行动画处理。 由于您想访问布局下方的内容,我建议您 使用ViewPropertyAnimator,那么你可以用一行来完成。

    一个最小的实现应该是这样的:

    rl_content.animate().translationX(0); // -> animates **TO** 0 on the X axis
    

    会将布局的左边框移动到屏幕的左侧。如果你想要更进一步,那么你需要一个负值。

    或者您可以使用 translationXBy() 方法 f.e. :

    rl_content.animate().translationXBy(-50);  // -> animates **BY** -50 on the X axis
    //(negative values are to the left)
    

    也很有趣:

    TranslateAnimation -> 在你的情况下不推荐,因为它并没有真正移动布局,只是让它看起来像在移动,所以你在访问 RelativeLayout 下的菜单时会遇到问题。

    ObjectAnimator

    【讨论】:

      猜你喜欢
      • 2015-07-01
      • 1970-01-01
      • 2012-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-04
      • 2017-07-04
      • 2012-07-10
      相关资源
      最近更新 更多