Android Studio 3.1.3在使用Constraint layout时存在问题

9
我正在尝试启动一个新项目,但出现了这个问题。我可以在模拟器中运行和部署该项目,但这个渲染问题和使用私有资源让我很烦恼。我已经尝试了互联网上所有可能的解决方案,但仍无法解决问题。

<?xml version="1.0" encoding="utf

<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"
   app:layout_behavior="@string/appbar_scrolling_view_behavior"
   tools:context=".MainActivity"
   tools:showIn="@layout/activity_main">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!" />

</android.support.constraint.ConstraintLayout>

样式

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</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" />

Build.gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.acer.myapplication3"
        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"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:design:28.0.0-alpha3'
    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'
}

请将您的XML文件源代码发送到这里。 - Learning Always
@LearningAlways 将其添加到描述中。 - Jeric Cabuyaban
你的问题是约束布局没有正确渲染吗?那么请提供你的 build.gradle 文件。 - Shweta Chauhan
清理重建和使无效/重新启动 - Vidhi Dave
2
https://dev59.com/LFoT5IYBdhLWcg3w8jFm#38777692 - Vidhi Dave
显示剩余6条评论
7个回答

10

我遇到了同样的问题-去SDK管理器并安装了额外的SDK平台;Oreo 8.1.看起来问题是“新”的API 28仍然存在问题。它说部分已安装,但我基本上只是选中了更低版本的API的框,并下载/安装:

我的API管理器的截图

此外,我更改了我的build.gradle文件的SDK版本,buildTools,appcompat和design版本如下所示。现在它可以正常工作,当这些问题得到解决后,我会回到API 28。

apply plugin: 'com.android.application'

android {
compileSdkVersion 27
buildToolsVersion '27.0.3'
defaultConfig {
    ...
    minSdkVersion 15
    targetSdkVersion 27
    ...

dependencies {
implementation 'com.android.support:appcompat-v7:27.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.android.support:design:27.0.0'
}

我认为发布版本也存在问题。我使用最新的金丝雀版毫不费力地启动了一个问题。无论如何,我会向你们更新真正的问题,我已经向谷歌创建了一个问题并向他们提出了错误。由于繁忙的日程安排,我现在没有时间回复他们或执行他们建议的修复操作。无论如何,感谢您的建议,我将在本周末尝试并向您更新结果。 - Jeric Cabuyaban
这应该是被接受的答案!我已经将我的Gradle构建版本从8更改为7。它起作用了!愉快编码! - Uzair Qaiser

2
您只需要更改您的build.gradle文件中依赖项部分的以下代码即可:

来自:

implementation 'com.android.support:appcompat-v7:28.0.0-rc02'
implementation 'com.android.support:design:28.0.0-rc02'

enter image description here

To:

implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'
implementation 'com.android.support:design:28.0.0-alpha1'

enter image description here

And then sync your project.


2
请勿将代码作为图像发布。您应该直接将代码剪切并粘贴到您的答案中。 - ehymel

1

我遇到了同样的问题。对我来说,原因是我的项目“自动将第三方库转换为使用AndroidX”。请检查您的问题是否与我的相同?如果是,请简单地按照描述此处的两个步骤操作。

为了使其更容易:

第一步: 请检查您的gradle.properties文件,如果您看到以下行,则可能与我的问题完全相同。您可以首先删除它们。

android.useAndroidX=true
android.enableJetifier=true

第二步:在主活动中,我修改了


import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

转换为

import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

突然间一切都正常了!

1
首先,如果您已经实现了androidX,然后又将其删除,您必须遵循这两个步骤。谢谢伙计。 - Nils

0
  1. 在左侧点击应用程序。
  2. 然后打开res文件夹。
  3. 打开values文件夹。
  4. 单击“styles.xml”文件夹。
  5. 将父变量更改为“Base.Theme.AppCompact.Light.DarkActionBar”

0
在 styles.xml 中,在“Theme.AppCompat.Light.DarkActionBar”之前添加“Base.”。 对我有用。

0
请确保在“设置 > Gradle”中取消选中离线选项,然后在联网的情况下再次同步项目。

0

尝试使用这个:

dependencies {
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.1'
}

不是使用 constraint-layout:1.1.2

这解决了我的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接