布局预览未呈现-可能是由于androidx.widget

时间:2019-02-09 20:58:18

标签: android android-studio layout kotlin

Android Studio中的布局预览无法渲染,我不确定为什么。我已将所有内容更新为最新版本,并且在其他计算机上似乎运行良好。我的教授不确定如何解决此问题,我可以找到的所有其他布局渲染堆栈溢出帖子都已过时。我已经尝试过刷新,重新启动,重建,使缓存无效等等。

我能得到的最好线索是,由于androidx.constraintlayout.widget.ConstraintLayout没有得到适当的支持。我的教授还提到Jetpack可能有问题?不太清楚他的意思。

enter image description here

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" android:background="@android:color/background_light">
    <EditText
            android:layout_width="86dp"
            android:layout_height="45dp"
            android:inputType="numberDecimal"
            android:ems="10"
            android:id="@+id/totalTwentyEditText"
            app:layout_constraintStart_toEndOf="@+id/totalFifteenEditText" app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="8dp"
            app:layout_constraintTop_toTopOf="@+id/guideline4" android:text="0.00" android:textAlignment="center"
            android:focusable="false" android:longClickable="false"/>
    <androidx.constraintlayout.widget.Guideline android:layout_width="wrap_content" android:layout_height="wrap_content"
                                                android:id="@+id/guideline" app:layout_constraintGuide_begin="20dp"
                                                android:orientation="horizontal"/>
    <androidx.constraintlayout.widget.Guideline android:layout_width="wrap_content" android:layout_height="wrap_content"
                                                android:id="@+id/guideline2" app:layout_constraintGuide_begin="86dp"
                                                android:orientation="horizontal"/>
    <androidx.constraintlayout.widget.Guideline android:layout_width="wrap_content" android:layout_height="wrap_content"
                                                android:id="@+id/guideline3" app:layout_constraintGuide_begin="128dp"
                                                android:orientation="horizontal"/>
    <androidx.constraintlayout.widget.Guideline android:layout_width="wrap_content" android:layout_height="wrap_content"
                                                android:id="@+id/guideline4"
                                                android:orientation="horizontal"
                                                app:layout_constraintGuide_begin="191dp"/>
    <androidx.constraintlayout.widget.Guideline android:layout_width="wrap_content" android:layout_height="wrap_content"
                                                android:id="@+id/guideline5" app:layout_constraintGuide_begin="259dp"
                                                android:orientation="horizontal"/>
    <androidx.constraintlayout.widget.Guideline android:layout_width="wrap_content" android:layout_height="wrap_content"
                                                android:id="@+id/guideline6" app:layout_constraintGuide_begin="309dp"
                                                android:orientation="horizontal"/>
    <androidx.constraintlayout.widget.Barrier
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/leftColumn"
            app:barrierDirection="end"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="16dp"
            app:constraint_referenced_ids="textView3,textView5,textView4,textView2,textView"/>
    <TextView
            android:text="@string/bill_total"
            android:layout_width="wrap_content"
            android:layout_height="23dp"
            android:id="@+id/textView" android:textSize="18sp"
            android:textStyle="bold"
            android:textAlignment="viewEnd"
            android:textColor="@android:color/black"
            android:layout_marginStart="16dp"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_marginTop="8dp"
            app:layout_constraintTop_toTopOf="@+id/guideline"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="@+id/billTotalEditText"/>
    <TextView
            android:text="@string/tip"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView2" android:textSize="18sp"
            android:textStyle="bold" android:textAlignment="viewEnd" android:textColor="@android:color/black"
            android:layout_marginEnd="8dp" app:layout_constraintEnd_toEndOf="@+id/leftColumn"
            android:layout_marginTop="8dp" app:layout_constraintTop_toTopOf="@+id/guideline3"
            android:layout_marginBottom="8dp" app:layout_constraintBottom_toBottomOf="@+id/tipTenEditText"/>
    <TextView
            android:text="@string/total"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView3" android:textSize="18sp"
            android:textStyle="bold" android:textAlignment="viewEnd" android:textColor="@android:color/black"
            android:layout_marginEnd="8dp" app:layout_constraintEnd_toEndOf="@+id/leftColumn"
            android:layout_marginTop="8dp" app:layout_constraintTop_toTopOf="@+id/guideline4"
            android:layout_marginBottom="8dp" app:layout_constraintBottom_toBottomOf="@+id/totalTenEditText"/>
    <TextView
            android:text="@string/custom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView4" android:textSize="18sp"
            android:textStyle="bold" android:textAlignment="viewEnd" android:textColor="@android:color/black"
            android:layout_marginEnd="8dp" app:layout_constraintEnd_toEndOf="@+id/leftColumn"
            android:layout_marginTop="8dp" app:layout_constraintTop_toTopOf="@+id/guideline5"
            android:layout_marginBottom="8dp" app:layout_constraintBottom_toBottomOf="@+id/customSeekBar"/>
    <TextView
            android:text="@string/tip"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView5" android:textSize="18sp"
            android:textStyle="bold" android:textAlignment="viewEnd" android:textColor="@android:color/black"
            android:layout_marginEnd="8dp" app:layout_constraintEnd_toEndOf="@+id/leftColumn"
            android:layout_marginTop="8dp" app:layout_constraintTop_toTopOf="@+id/guideline6"
            android:layout_marginBottom="8dp" app:layout_constraintBottom_toBottomOf="@+id/tipEditText"/>
    <EditText
            android:layout_width="302dp"
            android:layout_height="39dp"
            android:inputType="numberDecimal"
            android:ems="10"
            android:id="@+id/billTotalEditText"
            app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp" android:layout_marginTop="8dp"
            app:layout_constraintTop_toTopOf="@+id/guideline"
            android:layout_marginStart="8dp" app:layout_constraintStart_toStartOf="@id/leftColumn"
            app:layout_constraintHorizontal_bias="0.0" android:text="@string/zeroDecimal"/>
    <EditText
            android:layout_width="81dp"
            android:layout_height="wrap_content"
            android:inputType="numberDecimal"
            android:ems="10"
            android:id="@+id/tipTenEditText"
            app:layout_constraintStart_toEndOf="@+id/textView2" app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintEnd_toStartOf="@+id/tipFifteenEditText" android:layout_marginTop="8dp"
            app:layout_constraintTop_toTopOf="@+id/guideline3" android:text="@string/zeroDecimal"
            android:textAlignment="center" android:focusable="false" android:longClickable="false"/>
    <EditText
            android:layout_width="86dp"
            android:layout_height="wrap_content"
            android:inputType="numberDecimal"
            android:ems="10"
            android:id="@+id/tipFifteenEditText"
            app:layout_constraintStart_toEndOf="@+id/tipTenEditText" app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintEnd_toStartOf="@+id/tipTwentyEditText" android:layout_marginTop="8dp"
            app:layout_constraintTop_toTopOf="@+id/guideline3" android:text="@string/zeroDecimal"
            android:textAlignment="center" android:focusable="false" android:longClickable="false"/>
    <EditText
            android:layout_width="86dp"
            android:layout_height="wrap_content"
            android:inputType="numberDecimal"
            android:ems="10"
            android:id="@+id/tipTwentyEditText"
            app:layout_constraintStart_toEndOf="@+id/tipFifteenEditText" app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="8dp"
            app:layout_constraintTop_toTopOf="@+id/guideline3" android:text="@string/zeroDecimal"
            android:textAlignment="center" android:focusable="false" android:longClickable="false"/>
    <TextView
            android:text="@string/tenPercent"
            android:layout_width="36dp"
            android:layout_height="23dp"
            android:id="@+id/textView6" android:textSize="18sp"
            android:textAlignment="center" android:textStyle="bold" android:textColor="@android:color/black"
            android:layout_marginTop="8dp" app:layout_constraintTop_toTopOf="@+id/guideline2"
            app:layout_constraintStart_toStartOf="@+id/tipTenEditText" app:layout_constraintEnd_toEndOf="@+id/tipTenEditText"/>
    <TextView
            android:text="@string/fifteenPercent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView7" android:textSize="18sp"
            android:textAlignment="center" android:textStyle="bold" android:textColor="@android:color/black"
            android:layout_marginTop="8dp" app:layout_constraintTop_toTopOf="@+id/guideline2"
            app:layout_constraintStart_toStartOf="@+id/tipFifteenEditText" app:layout_constraintEnd_toEndOf="@+id/tipFifteenEditText"/>
    <TextView
            android:text="@string/twentyPercent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView8" android:textSize="18sp"
            android:textAlignment="center" android:textStyle="bold" android:textColor="@android:color/black"
            android:layout_marginTop="8dp" app:layout_constraintTop_toTopOf="@+id/guideline2"
            app:layout_constraintStart_toStartOf="@+id/tipTwentyEditText" app:layout_constraintEnd_toEndOf="@+id/tipTwentyEditText"/>
    <EditText
            android:layout_width="80dp"
            android:layout_height="45dp"
            android:inputType="numberDecimal"
            android:ems="10"
            android:id="@+id/totalTenEditText"
            app:layout_constraintStart_toEndOf="@+id/textView3" app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintEnd_toStartOf="@+id/totalFifteenEditText" android:layout_marginTop="8dp"
            app:layout_constraintTop_toTopOf="@+id/guideline4" android:text="@string/zeroDecimal"
            android:textAlignment="center" android:focusable="false" android:longClickable="false"/>
    <EditText
            android:layout_width="86dp"
            android:layout_height="45dp"
            android:inputType="numberDecimal"
            android:ems="10"
            android:id="@+id/totalFifteenEditText"
            app:layout_constraintStart_toEndOf="@+id/totalTenEditText" app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintEnd_toStartOf="@+id/totalTwentyEditText" android:layout_marginTop="8dp"
            app:layout_constraintTop_toTopOf="@+id/guideline4" android:text="@string/zeroDecimal"
            android:textAlignment="center" android:focusable="false" android:longClickable="false"/>
    <SeekBar
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/customSeekBar" android:layout_marginTop="8dp"
            app:layout_constraintTop_toTopOf="@+id/guideline5" app:layout_constraintEnd_toStartOf="@+id/customTextView"
            android:layout_marginEnd="8dp" app:layout_constraintStart_toEndOf="@+id/leftColumn"
            android:layout_marginStart="8dp" android:progress="18" android:max="100"/>
    <TextView
            android:text="@string/eighteenPercent"
            android:layout_width="55dp"
            android:layout_height="29dp"
            android:id="@+id/customTextView" android:textSize="18sp"
            android:textAlignment="center" android:textStyle="bold" android:textColor="@android:color/black"
            android:layout_marginTop="8dp" app:layout_constraintTop_toTopOf="@+id/guideline5"
            app:layout_constraintStart_toStartOf="@+id/totalTwentyEditText" app:layout_constraintEnd_toEndOf="@+id/totalTwentyEditText"
            android:layout_marginBottom="8dp" app:layout_constraintBottom_toBottomOf="@+id/customSeekBar"/>
    <EditText
            android:layout_width="84dp"
            android:layout_height="43dp"
            android:inputType="numberDecimal"
            android:ems="10"
            android:id="@+id/tipEditText" android:layout_marginTop="8dp"
            app:layout_constraintTop_toTopOf="@+id/guideline6"
            app:layout_constraintEnd_toEndOf="@+id/totalTenEditText"
            app:layout_constraintStart_toStartOf="@+id/totalTenEditText"
            android:text="@string/zeroDecimal" android:textAlignment="center" android:focusable="false"
            android:longClickable="false"/>
    <TextView
            android:text="@string/total"
            android:layout_width="51dp"
            android:layout_height="26dp"
            android:id="@+id/textView10" android:textSize="18sp"
            android:textStyle="bold" android:textAlignment="viewEnd" android:textColor="@android:color/black"
            android:layout_marginTop="8dp" app:layout_constraintTop_toTopOf="@+id/guideline6"
            android:layout_marginBottom="8dp" app:layout_constraintBottom_toBottomOf="@+id/tipEditText"
            app:layout_constraintStart_toStartOf="@+id/totalFifteenEditText" app:layout_constraintEnd_toEndOf="@+id/totalFifteenEditText"/>
    <EditText
            android:layout_width="86dp"
            android:layout_height="47dp"
            android:inputType="numberDecimal"
            android:ems="10"
            android:id="@+id/totalEditText" android:layout_marginTop="8dp"
            app:layout_constraintTop_toTopOf="@+id/guideline6"
            app:layout_constraintStart_toStartOf="@+id/totalTwentyEditText"
            app:layout_constraintEnd_toEndOf="@+id/totalTwentyEditText"
            app:layout_constraintBottom_toBottomOf="@+id/textView10" android:text="@string/zeroDecimal"
            android:textAlignment="center" android:focusable="false" android:longClickable="false"/>
</androidx.constraintlayout.widget.ConstraintLayout>

应用gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

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

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    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'
}

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

我可以看到您的实现是错误的:

com.android.support.constraint:constraint-layout:1.1.3

您应该具有:

androidx.constraintlayout:constraintlayout:2.0.0-alpha3

还有其他一些类似的东西:

com.android.support:appcompat-v7:27.1.1

修复所有goto Refactor(菜单)-> migrateToAndroidX 如果没有这样的选项,请更新您的ide(Android Studio)。

答案 1 :(得分:0)

  1. 您可以转到“折射菜单”,然后->单击“迁移到Android” X
  2. 这些依赖项将自动添加,如果没有,则可以添加它们(注意:依赖项版本可能有所不同)

    实现'androidx.appcompat:appcompat:1.0.2'

    实现'androidx.constraintlayout:constraintlayout:1.1.3'

    实现'com.google.firebase:firebase-auth:16.2.1'

    实现'androidx.legacy:legacy-support-v4:1.0.0'

    testImplementation'junit:junit:4.12'

    androidTestImplementation'androidx.test:runner:1.1.1'

    androidTestImplementation'androidx.test.espresso:espresso-core:3.1.1'

  3. 在预览中尝试使用“布局编辑器”中的“强制刷新布局”选项。

  4. 如果第三个选项没有帮助,请尝试清理并重建项目。这应该可以解决问题。

我知道这个问题已经有了答案,但是当我使用这些步骤解决相同的问题时,这些步骤可能会对某人有所帮助。