Android Studio 3.1.3,设计视图始终为空

Adn*_*YAS 21 android android-layout android-studio

我是Android开发的新手,我的Android Studio 3.1.3演示项目或我创建的任何项目都有这个问题.

即使我可以在我的设计视图(ConstraintLayout)上拖放不同的控件但在设计视图中没有显示任何控件,它会显示一段时间然后消失.

我可以在activity_main.xml文本选项卡,组件树甚至运行模式中看到我拖放的所有元素,但是在设计选项卡中没有显示,设计视图中没有显示任何元素,它总是空白,甚至没有hello world show.

我尝试了无效缓存重启,重启我的电脑,更改放大 - 进出,推断约束,但没有运气.控制

除非我在设计视图中看到东西,否则我做不了多少.

空设计视图:

空设计视图

运行模式视图

运行模式视图

activity_main.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=".MainActivity">

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:text="Button" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="20dp" />

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox"
        tools:layout_editor_absoluteX="154dp"
        tools:layout_editor_absoluteY="181dp" />

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.libra.myapplication">

    <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">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
Run Code Online (Sandbox Code Playgroud)

的build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.libra.myapplication"
        minSdkVersion 15
        targetSdkVersion 28
        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:28.0.0-alpha3'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    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'
}
Run Code Online (Sandbox Code Playgroud)

Adn*_*YAS 35

好的,我的问题解决了!

无法实例化以下类: - android.support.v7.widget.Toolbar

我从这里更改了res/values/styles.xml文件:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
Run Code Online (Sandbox Code Playgroud)

对此:

<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
Run Code Online (Sandbox Code Playgroud)

这解决了这个问题

  • 这是我朋友的正确答案!我不知道Google如何做好事情。 (2认同)

Tam*_*ynh 0

您的小部件布局有问题CheckBox。它没有任何限制。tools:layout_editor_absoluteX并且tools:layout_editor_absoluteY仅影响设计模式,但不影响实际运行的应用程序。

注意:不要在设计视图上拖/放小部件,它会生成许多奇怪的属性,这些属性并非在所有情况下都有效,就像您获得的两个属性一样。而是在代码中执行此操作。如果你想让CheckBox位于中心,请这样做:

<CheckBox
    android:id="@+id/checkBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="CheckBox"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent" />
Run Code Online (Sandbox Code Playgroud)

这将删除 处的错误图标CheckBox,然后刷新可能会有所帮助。