0

我切换到约束布局的新更新,这就是我得到的,只是一个白屏

在此处输入图像描述

想也许我在我的片段中做错了什么,但不,它没有显示任何错误。所以我试图检查布局,这就是我得到的

在此处输入图像描述

它为文本编辑器创造了空间,但它不绘制任何东西。这是我的 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/fragment_login"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/Plum"
    android:clickable="true"
    tools:context=".ui.LoginFragment">

    <EditText
        android:id="@+id/login_username"
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:hint="@string/Username_or_Email"
        android:inputType="textNoSuggestions|textEmailAddress"
        android:lines="1"
        android:linksClickable="false"
        app:layout_constraintBottom_toTopOf="@+id/login_password"
        app:layout_constraintRight_toRightOf="@+id/login_password" />

    <EditText
        android:id="@+id/login_password"
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:hint="@string/password"
        android:inputType="textPassword"
        android:lines="1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/login_login"
        style="@style/Base.Widget.AppCompat.Button.Colored.login"
        android:layout_width="150dp"
        android:layout_height="50dp"
        android:text="@string/login"
        app:layout_constraintLeft_toLeftOf="@+id/login_password"
        app:layout_constraintRight_toRightOf="@+id/login_password"
        app:layout_constraintTop_toBottomOf="@+id/login_password" />

    <Button
        android:id="@+id/login_singup"
        style="@style/Base.Widget.AppCompat.Button.Colored.signup"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:background="@color/Indigo"
        android:clickable="true"
        android:gravity="center"
        android:lines="1"
        android:text="@string/singup"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

</android.support.constraint.ConstraintLayout>

我不确定是什么导致了问题。它与 alpha 7 完美配合:/有什么帮助吗?

编辑:

片段是这样创建的:

mFragmentManager = getSupportFragmentManager();
        mFragmentTransaction = mFragmentManager.beginTransaction();
        switch (fragmentName) {
            case "LoginFragment":
                Log.d(KEY_CLASS_NAME, "createFragment-LoginFragment");
                fragment = new LoginFragment();
                mFragmentTransaction.setCustomAnimations(
                        R.anim.icon_anim_fade_in,
                        R.anim.icon_anim_fade_out,
                        R.anim.icon_anim_fade_in,
                        R.anim.icon_anim_fade_out)
                        .add(R.id.dispatcher_container, fragment, SPKey.KEY_LOGIN_FRAGMENT)
                        .addToBackStack(SPKey.KEY_LOGIN_FRAGMENT)
                        .commit();
                break;

OnCreateView() 是在 LoginFragment 内部实现的:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        mView = inflater.inflate(R.layout.fragment_login, container, false);
        if (mView != null) {
            Log.d("LoginFragment", "creating ver");
            mUsername = (EditText) mView.findViewById(R.id.login_username);
            mPassword = (EditText) mView.findViewById(R.id.login_password);
            mLogin = (Button) mView.findViewById(R.id.login_login);
            mSignUp = (Button) mView.findViewById(R.id.login_singup);
            mLogin.setOnClickListener(this);
            mSignUp.setOnClickListener(this);
            Log.d("LoginFragment", "done creating ver");
        } else {
            Log.d("LoginFragment", "mView is empty");
        }
        return mView;
    }

此外,我刚刚发现 Android Studio 2.2.1 中的 SDK 管理器在“~\Android\sdk\extras”中下载“m2repository”,但如果您使用独立 SDK 管理器,它会将“m2repository”放在“~\Android\ sdk\extras\android"。我不知道这是否重要,但这就是约束布局文件夹所在的位置。

4

3 回答 3

1

下面是使用约束布局的例子,

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"
                                             android:layout_width="match_parent"
                                             android:layout_height="match_parent"
                                             android:layout_alignParentLeft="true"
                                             android:layout_alignParentStart="true"
                                             android:layout_alignParentTop="true"
                                             android:layout_margin="16dp">

<EditText
    android:id="@+id/login_username"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="Email"
    android:inputType="textNoSuggestions|textEmailAddress"
    android:lines="1"
    android:linksClickable="false"/>

<EditText
    android:id="@+id/login_password"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="Password"
    android:inputType="textPassword"
    android:lines="1"
    app:layout_constraintTop_toBottomOf="@+id/login_username"/>

<android.support.v7.widget.AppCompatButton
    android:id="@+id/login_login"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Login"
    app:layout_constraintTop_toBottomOf="@+id/login_password"/>

<android.support.v7.widget.AppCompatButton
    android:id="@+id/login_singup"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:lines="1"
    android:text="Signup"
    app:layout_constraintLeft_toRightOf="@+id/login_login"
    app:layout_constraintTop_toBottomOf="@+id/login_password"/>
</android.support.constraint.ConstraintLayout>

和输出,

https://i.stack.imgur.com/iCtwm.png

希望这会帮助你

干杯!!!

于 2016-10-15T05:27:27.343 回答
1

是的,愚蠢的错误。将约束布局降级到 7,然后它应该可以工作

去你的gradle并替换

编译'com.android.support.constraint:constraint-layout:1.0.0-alpha9'

编译'com.android.support.constraint:constraint-layout:1.0.0-alpha7'

不知道为什么这可以解决

于 2016-10-17T00:42:07.710 回答
0

我们刚刚发布了 ConstraintLayout beta 1,它修复了几个可能解释了您的错误的问题。

虽然我无法复制您的确切问题,但请更新并检查 beta 1 是否解决了它——我很想确认是这样。

谢谢!

于 2016-10-20T18:13:58.533 回答