为什么不执行XML指定的转换? (或者为什么不考虑指定的持续时间?)

问题描述 投票:0回答:1

我遵循了这个文档:https://developer.android.com/training/transitions/start-activity#java

首先,我将向您展示我的实施。你会在这篇文章的最后找到我的问题。

因此,我修改了4个文件:

  1. fade.xml,定义转换中淡入淡出的持续时间。
<?xml version="1.0" encoding="utf-8"?>
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
    <fade android:fadingMode="fade_in" android:duration="3000" />
</transitionSet>
  1. values / styles.xml,由于fade.xml定义了windowEnterTransition。请注意,MainActivity的父级AppTheme将Material作为父级。

<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Material">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <item name="android:windowActivityTransitions">true</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

<style name="MainActivityTheme" parent="AppTheme">
    <item name="android:windowEnterTransition">@transition/fade</item>
</style>

  1. 自从我创建了这个新主题“MainActivityTheme”之后,我还修改了Android Manifest,为此活动指定了这个新主题:
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity"
        android:theme="@style/MainActivityTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
  1. 最后,我在MainActivity.java中设置了enter转换:
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setEnterTransition(new Fade(Fade.IN));
    setContentView(R.layout.activity_main);
}

Emulator and Gradle build

build.gradle(模块:app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.example.."
        minSdkVersion 21
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:support-v4:27.1.1'
    implementation 'com.android.support:design:27.1.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

模拟器

安装在模拟器中的Android版本是:5.1.1(参见Android设置中的“关于手机”,在模拟器中)。

Question

但是,当我启动我的应用程序时,主要活动的UI不会淡入。你知道为什么吗?换句话说:为什么不执行XML指定的转换? (或者为什么不考虑指定的持续时间?)

我还尝试将<item name="android:windowActivityTransitions">true</item>从主题MainActivityTheme(文件:styles.xml)移动到主题AppTheme(文件:相同,即:styles.xml)。但它没有解决这个问题。

android android-transitions
1个回答
0
投票

试试这个我希望它的工作

材料设计应用程序中的活动转换通过共同元素之间的运动和转换提供不同状态之间的视觉连接。您可以为进入和退出过渡以及共享元素between activities.的过渡指定自定义动画

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
    setContentView(R.layout.activity_main);
}

也读这个问题今年五月帮助你WindowEnterTransition Not Affecting Activity Transition

© www.soinside.com 2019 - 2024. All rights reserved.