我是 Android 开发新手,我的 Android Studio 3.1.3 演示项目或我创建的任何项目都遇到此问题。
尽管我可以在设计视图(ConstraintLayout)上拖放不同的控件,但设计视图上没有显示任何内容,它会显示一瞬间然后消失。
我可以在 Activity_main.xml 文本选项卡、组件树甚至运行模式中看到拖放的所有元素,但在设计选项卡中看不到,设计视图中没有显示任何内容,并且它始终是空白的,甚至连 hello world 都没有显示在上面。
我尝试过无效缓存重新启动、重新启动我的电脑、更改放大和缩小、推断约束,但没有运气。控制
除非我在设计视图中看到东西,否则我无能为力。
空设计视图:
运行模式视图
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>
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>
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'
}
好的我的问题解决了!
以下类无法实例化: - android.support.v7.widget.Toolbar
我更改了 res/values/styles.xml 文件:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
对此:
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
问题就解决了
另一个解决方法是编辑 GradleScripts/build.gradel(Module app)
-> 实现 'com.android.support:appcompat-v7:28.0.0-alpha3' 进入实现'com.android.support:appcompat-v7:28.0.0-alpha1'
如果有的话,
-> 实现 'com.android.support:design:28.0.0-alpha3' 进入实现'com.android.support:design:28.0.0-alpha1'
我也遇到了同样的问题,并发现这个技巧对我有用。我已将位于 Gradle Scripts/ build.gradle(Module:app) 中的应用程序文件中的版本更改为版本 27。
CompileSdkVersion
、minSdkVersion
、targetSdkVersion
和 implementation 'com.android.support:appcompat-v7:27.0.0'
。这些列在第 4、7、8 和 23 行..(对您来说可能会有所不同)。
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.fiv4.masterapp"
minSdkVersion 27
targetSdkVersion 27
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:27.0.0'
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'
}
使用稳定版本的支持库。
问题是当您使用不稳定的 alpha 支持库时。在 google maven repo 上查看稳定的库版本。
回答时,
support:appcomact-v7
最稳定的版本是28.0.0-rc02
更换
implementation 'com.android.support:appcompat-v7:28.0.0-alpha3' // less stable alpha
与
implementation 'com.android.support:appcompat-v7:28.0.0-rc02' // more stable
同步,重启AS即可。问题解决了!
您的
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" />
这将删除
CheckBox
处的错误图标,然后刷新可能会有所帮助。
转到 res/values/styles.xml 文件...
4号线,总是...
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
更改为
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
无需保存文件,会自动保存。转到 Activity_main.xml --> 设计(视图)...现在应该显示它了。