setTranslation在onCreate()上不起作用

伊利亚尔·阿巴德(Elyar Abad)

setTranslationY()不会在中的布局上执行翻译onCreate(),而在setonTouchListener或中使用时则没有问题setOnClickListener

我也试过了animate().translationY()但是,出现相同的确切问题。为了使问题更清楚,我尝试提出需要的代码部分。活动中包括布局。此布局包含另一个布局,其高度用于翻译度量中。

Java代码:

View mainMenuIncluded, mainMenu ;
mainMenuIncluded = findViewById(R.id.main_menu_included);
mainMenu = mainMenuIncluded.findViewById(R.id.main_menu);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pick_singer_list);
        mainMenuIncluded.setTranslationY(mainMenuIncluded.getY()-mainMenu.getHeight());
    }

活动的相关xml部分:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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"
    tools:context=".activity">
<include
            layout="@layout/layout_pick_main_menu"
            android:id="@+id/main_menu_included"/>

    </android.support.constraint.ConstraintLayout>

所包含布局的XML(containig mainMenu):

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="@+id/main_menu_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="0dp">
<android.support.constraint.ConstraintLayout
        android:id="@+id/main_menu_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="0dp"
        android:layout_marginBottom="0dp"
        android:background="@drawable/menu_gradient_background"
        app:layout_constraintBottom_toTopOf="@id/main_menu_button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.constraint.ConstraintLayout
            android:id="@+id/main_menu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="0dp"
            android:layout_marginBottom="0dp"
            android:background="@drawable/menu_gradient_background"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            >
        </android.support.constraint.ConstraintLayout>

    </android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>
彼得·斯塔南丘克

仅当已绘制和测量视图时,才能对其进行动画处理和移动。采用:

Kotlin版本:

yourView.post { 
    yourView.animate().translationY(/*value*/)            
}

Java版本:

yourView.post(new Runnable() {
    @Override
    public void run() {
        yourView.animate().translationY(/*value*/);
    }
};

为了这

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章