android - setTranslation 不适用于 onCreate()

标签 android android-view

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

我也尝试过 animate().translationY()。但是出现了同样的问题。 我试图展示我的代码中需要使问题更清楚的部分。布局包含在 Activity 中。此布局包含另一个布局,其高度用于平移测量。

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

Activity 的相关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(包含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>

最佳答案

只有当 View 已经绘制和测量时,您才能对 View 进行动画处理和移动。 使用:

Kotlin 版本:

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

Java 版本:

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

为此

关于android - setTranslation 不适用于 onCreate(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57478957/

相关文章:

android - 未能找到 : com. android.support :appcompat-v7:22. 2.0

android - 具有图层列表背景可绘制的自定义 View 呈现黑屏

android - 在android中的显示器(屏幕)上查找 View 位置(位置)

android - 更改 Android 应用程序的 View 而不是模拟器

android - 如何测试自定义 View 性能

android - 是否可以在 AppBarLayout 中折叠多个 View ?

android - 如何将我的 Android 代码转换为我的 Firebase 函数的 Swift 代码?

Android Sqlite 没有结果

android - 从非 UI 线程更新 View

android - 动态设置语言字符串源