Android-在动画后添加子视图

Android-在动画后添加子视图,android,android-layout,animation,Android,Android Layout,Animation,我正在尝试在动画后的另一个视图下添加子视图。有人知道我做错了什么吗 this.animate().translationY(newY - 170).setDuration(500).withEndAction(new Runnable() { @Override public void run() { clearAnimation(); RouteBreakDownLayout routeBreakDownLayo

我正在尝试在动画后的另一个视图下添加子视图。有人知道我做错了什么吗

this.animate().translationY(newY - 170).setDuration(500).withEndAction(new Runnable() {
        @Override
        public void run() {
            clearAnimation();

            RouteBreakDownLayout routeBreakDownLayout = new RouteBreakDownLayout(mContext);
            RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);

            p.addRule(RelativeLayout.BELOW, R.id.selected_route_layout);
            routeBreakDownLayout.setLayoutParams(p);
            addView(routeBreakDownLayout);

        }
    }).start();
下面是布局的XML。我有一个滚动视图嵌入其中将举行的内容,需要在动画

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/selected_route_layout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="vertical">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@+id/selected_route_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_gravity="bottom"
        android:layout_alignParentBottom="true"
        android:background="#FFF"
        android:gravity="bottom"
        android:padding="10dip">

        <TextView
            android:id="@+id/time_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="10dp"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#1D1C18" />

        <TextView
            android:id="@+id/eta_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/time_label"
            android:layout_toEndOf="@+id/time_label"
            android:layout_toRightOf="@+id/time_label"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#7EC82F" />

        <LinearLayout
            android:id="@+id/route_breakdown"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/time_label"
            android:layout_centerVertical="true"
            android:layout_marginBottom="10dip"
            android:layout_marginTop="10dip"
            android:gravity="center_vertical"
            android:orientation="horizontal" />

        <TextView
            android:id="@+id/via_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/route_breakdown"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#BDBDBD" />

        <ImageButton
            android:id="@+id/arrow_up"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:background="@null"
            android:padding="15dip"
            android:src="@drawable/transit_symbol_up_arrow" />

    </RelativeLayout>

    <ScrollView
        android:layout_below="@+id/selected_route_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:showDividers="middle"
            android:divider="?android:dividerHorizontal"
            android:animateLayoutChanges="true"
            android:paddingLeft="16dp"
            android:paddingRight="16dp" />

    </ScrollView>

</RelativeLayout>


您的代码片段出了什么问题

下面是我提出的工作示例:

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

    <View
        android:id="@+id/headerView"
        android:background="#00FF00"
        android:layout_width="match_parent"
        android:layout_height="25dp"/>

    <View
        android:id="@+id/detailsView"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:clickable="true"
        android:background="#FF0000"/>

</RelativeLayout>
它就像预期的那样工作


您是否尝试在没有动画的情况下添加控件(RouteBreakDownLayout)?它有用吗?它可以是控件实际添加的东西,但它不在视口中,等等-到目前为止,我只能猜测。

您的代码片段中出了什么问题

下面是我提出的工作示例:

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

    <View
        android:id="@+id/headerView"
        android:background="#00FF00"
        android:layout_width="match_parent"
        android:layout_height="25dp"/>

    <View
        android:id="@+id/detailsView"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:clickable="true"
        android:background="#FF0000"/>

</RelativeLayout>
它就像预期的那样工作


您是否尝试在没有动画的情况下添加控件(RouteBreakDownLayout)?它有用吗?它可能有点像控件实际上在添加,但它不在视口中,等等。-到目前为止,我只能猜测。

我将布局更改为线性布局,但它仍然没有添加RouteBreakdown布局。如果我在顶部绘制父布局,android:layout\u alignParentBottom=“false”并使用动画绘制RouteBreakDown,效果会很好,但我需要在动画完成后显示RouteBreakDown。@Mikerizzo 1)纠正我,如果我错了:您试图在该项下绘制新项,哪个设置为android:layout\u alignParentBottom=“true”?如果您也在这里发布XML,则更容易理解。2) 尝试运行与在run()方法中编写的代码相同的代码,而不使用动画。它工作吗?是的,你是正确的,但我想画新的项目时,动画完成。我已经在原来的问题中添加了xml。@Mikerizzo问题是先让它在没有动画的情况下工作,然后让它在动画的情况下工作。你能再检查一下它是否在没有动画的情况下工作吗?并且您不能将项目“添加到”该项目下,即android:layout\u alignParentBottom=“true”。因为那个已经在底部了。因此,您的项目可能位于应用程序的ViewHierarchy中,但在ViewPort之外,顺便说一句,您可以在hierarchy Viewer()中检查您的视图层次结构,以确保已添加小部件。我将布局更改为线性布局,但仍然没有添加RouteBreakdown布局。如果我在顶部绘制父布局,android:layout\u alignParentBottom=“false”并使用动画绘制RouteBreakDown,效果会很好,但我需要在动画完成后显示RouteBreakDown。@Mikerizzo 1)纠正我,如果我错了:您试图在该项下绘制新项,哪个设置为android:layout\u alignParentBottom=“true”?如果您也在这里发布XML,则更容易理解。2) 尝试运行与在run()方法中编写的代码相同的代码,而不使用动画。它工作吗?是的,你是正确的,但我想画新的项目时,动画完成。我已经在原来的问题中添加了xml。@Mikerizzo问题是先让它在没有动画的情况下工作,然后让它在动画的情况下工作。你能再检查一下它是否在没有动画的情况下工作吗?并且您不能将项目“添加到”该项目下,即android:layout\u alignParentBottom=“true”。因为那个已经在底部了。因此,您的项目可能位于应用程序的ViewHierarchy中,但在ViewPort之外,顺便说一句,您可以在hierarchy Viewer()中检查您的视图层次结构,以确保已添加小部件。这会让你更清楚地了解出了什么问题。