约束布局 beta 4 - 片段不起作用

Constraint layout beta 4 - fragments don't work

备注0: 至少试试这个代码,让我们知道它是否适合你。因为理论上这应该可行,但实际上行不通

注1: 这段代码没有错误,如果您在 alpha-7 以上的任何 Constraint 布局上,它根本不起作用。但是,我将 logcat.

MainActivity.java:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentManager mFragmentManager = getSupportFragmentManager();
        FragmentTransaction mFragmentTransaction = mFragmentManager.beginTransaction();
        Fragment fragment = new stupid();
        mFragmentTransaction
                .add(R.id.main_activity, fragment, "stupid")
                .addToBackStack("stupid")
                .commit();

    }

}

activity_main.xml:

<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_activity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    android:clickable="true"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <TextView
        android:text="Hi 1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteY="13dp"
        tools:layout_editor_absoluteX="18dp"
        android:id="@+id/textView" />
</android.support.constraint.ConstraintLayout>

Stupid.java:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 */
public class stupid extends Fragment {


    public stupid() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_stupid, container, false);
    }

}

fragment_stupid.xml:

<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="xxx.xxxxxxx.xxxxxxxx.xxxxxxxxxxxxx.stupid">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_blank_fragment"
        android:id="@+id/textView2"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="136dp" />

</android.support.constraint.ConstraintLayout>

日志:

12-04 10:28:30.965 12609-12609/xxx.xxxxxxx.xxxxxxxx.xxxxxxxxxxxxx I/art: Late-enabling -Xcheck:jni
12-04 10:28:31.015 12609-12609/xxx.xxxxxxx.xxxxxxxx.xxxxxxxxxxxxx I/InstantRun: Instant Run Runtime started. Android package is xxx.xxxxxxx.xxxxxxxx.xxxxxxxxxxxxx, real application class is null.
12-04 10:28:31.299 12609-12609/xxx.xxxxxxx.xxxxxxxx.xxxxxxxxxxxxx W/art: Failed to find OatDexFile for DexFile /data/data/xxx.xxxxxxx.xxxxxxxx.xxxxxxxxxxxxx/files/instant-run/dex/slice-slice_1-classes.dex ( canonical path /data/data/xxx.xxxxxxx.xxxxxxxx.xxxxxxxxxxxxx/files/instant-run/dex/slice-slice_1-classes.dex) with checksum 0xe09cbad0 in OatFile /data/data/xxx.xxxxxxx.xxxxxxxx.xxxxxxxxxxxxx/cache/slice-slice_1-classes.dex
12-04 10:28:31.512 12609-12609/xxx.xxxxxxx.xxxxxxxx.xxxxxxxxxxxxx I/LoadedApk: No resource references to update in package common
12-04 10:28:31.512 12609-12609/xxx.xxxxxxx.xxxxxxxx.xxxxxxxxxxxxx I/LoadedApk: No resource references to update in package com.brit.swiftdark
12-04 10:28:31.809 12609-12609/xxx.xxxxxxx.xxxxxxxx.xxxxxxxxxxxxx W/ResourceType: For resource 0x01030224, entry index(548) is beyond type entryCount(29)
12-04 10:28:31.809 12609-12609/xxx.xxxxxxx.xxxxxxxx.xxxxxxxxxxxxx W/ResourceType: For resource 0x01030224, entry index(548) is beyond type entryCount(29)
12-04 10:28:31.843 12609-12609/xxx.xxxxxxx.xxxxxxxx.xxxxxxxxxxxxx W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
12-04 10:28:31.947 12609-12726/xxx.xxxxxxx.xxxxxxxx.xxxxxxxxxxxxx D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
12-04 10:28:31.956 12609-12609/xxx.xxxxxxx.xxxxxxxx.xxxxxxxxxxxxx D/Atlas: Validating map...
12-04 10:28:31.996 12609-12726/xxx.xxxxxxx.xxxxxxxx.xxxxxxxxxxxxx I/Adreno: QUALCOMM build                   : 065751b, 
                                                                            Build Date                       : 04/15/15
                                                                            OpenGL ES Shader Compiler Version: E031.25.03.07
                                                                            Local Branch                     : 
                                                                            Remote Branch                    : quic/LA.BF64.1.2.1_rb2.9
                                                                            Remote Branch                    : NONE
                                                                            Reconstruct Branch               : AU_LINUX_ANDROID_LA.BF64.1.2.1_RB2.05.01.00.081.016 + 065751b +  NOTHING
12-04 10:28:32.003 12609-12726/xxx.xxxxxxx.xxxxxxxx.xxxxxxxxxxxxx I/OpenGLRenderer: Initialized EGL, version 1.4
12-04 10:28:32.011 12609-12726/xxx.xxxxxxx.xxxxxxxx.xxxxxxxxxxxxx D/OpenGLRenderer: Enabling debug mode 0
12-04 10:28:32.069 12609-12609/xxx.xxxxxxx.xxxxxxxx.xxxxxxxxxxxxx I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@22f2f04f time:200835125

编辑 1: 阿尔法7

Beta-4

请参阅此post 以获得答案....

我知道这两个"This post and the linked post"都是我的,也是答案。但我一直在寻找答案近一年。我终于发现,出于某种原因,旧版本的 ConstraintLayout 将被添加到 activity 之上。较新版本的 ConstraintLayout 片段将放在 activity 的后面。或者至少在我看来是这样。这就是为什么我说让你的 activity 透明。