约束布局。渲染问题。布局编辑器
Constraint Layout. Render problems. Layout Editor
我每天都在项目中遇到非常令人沮丧的 ConstraintLayout 问题。
项目 gradle 文件:
buildscript {
ext.support_library_version = '27.0.2'
ext.play_services_version = '11.6.2'
ext.firebase_version = '11.6.2'
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'io.realm:realm-gradle-plugin:4.2.0'
classpath 'com.google.gms:google-services:3.1.2'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
google()
maven { url "https://maven.google.com" }
maven { url 'https://jitpack.io' }
}
}
这是我的短版应用gradle:
apply plugin: 'com.android.library'
apply plugin: 'realm-android'
// set to 'cmake', 'none'
def nativeBuildSystem = 'none'//'cmake'
realm {
syncEnabled = false
}
android {
defaultConfig {
targetSdkVersion 27
compileSdkVersion 27
minSdkVersion 21
// for using vectors for less than 21 API
vectorDrawables.useSupportLibrary = true
versionCode 56
dexOptions {
preDexLibraries true
javaMaxHeapSize "4g"
}
// Enabling multidex support.
multiDexEnabled true
versionName "1.6.5"
renderscriptTargetApi 2
renderscriptSupportModeEnabled true // Enable RS support
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
configurations {
all*.exclude group: 'commons-logging', module: 'commons-logging'
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
/********************************* Deep Linking Library *********************************************/
api 'com.airbnb:deeplinkdispatch:3.1.1'
annotationProcessor 'com.airbnb:deeplinkdispatch-processor:3.1.1'
/********************************* Android SDK *********************************************/
api "com.android.support:appcompat-v7:$support_library_version"
api 'com.android.support.constraint:constraint-layout:1.0.2'
api "com.android.support:percent:$support_library_version"
api "com.android.support:recyclerview-v7:$support_library_version"
api "com.android.support:gridlayout-v7:$support_library_version"
api "com.android.support:cardview-v7:$support_library_version"
api "com.android.support:design:$support_library_version"
api "com.android.support:support-vector-drawable:$support_library_version"
api "com.android.support:support-core-utils:$support_library_version"
api 'com.android.support:multidex:1.0.2'
}
低于我在布局编辑器中看到的内容:
下面我的xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.constraint.ConstraintLayout>
布局编辑器在这种情况下说:
如果我点击 显示异常,我将看到:
java.lang.NegativeArraySizeException
at android.support.constraint.R$styleable.<clinit>(Unknown Source)
at android.support.constraint.ConstraintLayout.init(ConstraintLayout.java:440)
at android.support.constraint.ConstraintLayout.<init>(ConstraintLayout.java:420)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:481)
at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:264)
at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:222)
at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:211)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:337)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:348)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:248)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:325)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:384)
at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:193)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:547)
at com.android.tools.idea.rendering.RenderTask.lambda$inflate(RenderTask.java:681)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
为了解决这个问题我尝试了很多但都没有成功。我没有使用简单的方法解决问题,例如使 cache/clean project/rebuild 项目无效等...
问题:
如果在 attr.xml 中定义的一个 <declare-stylable>
标签中有 太多 属性,布局编辑器似乎会遇到一些麻烦!
解法:
我在一个 <declare-stylable>
标签中有 49 个属性。
如果我删除 4 属性,那么我有 45,布局编辑器正在工作。
我还在 Google 问题跟踪器上添加了一个问题:
https://issuetracker.google.com/issues/71641021
更新自 Google 问题:
此问题已在 Android Studio 3.1 版本中修复。
不要使用 ConstraintLayout
,而是使用 Relative...
或 Linear...
。那应该可以。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical">
<EditText
android:layout_width="match_parent" />
</LinearLayout>
我每天都在项目中遇到非常令人沮丧的 ConstraintLayout 问题。
项目 gradle 文件:
buildscript {
ext.support_library_version = '27.0.2'
ext.play_services_version = '11.6.2'
ext.firebase_version = '11.6.2'
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'io.realm:realm-gradle-plugin:4.2.0'
classpath 'com.google.gms:google-services:3.1.2'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
google()
maven { url "https://maven.google.com" }
maven { url 'https://jitpack.io' }
}
}
这是我的短版应用gradle:
apply plugin: 'com.android.library'
apply plugin: 'realm-android'
// set to 'cmake', 'none'
def nativeBuildSystem = 'none'//'cmake'
realm {
syncEnabled = false
}
android {
defaultConfig {
targetSdkVersion 27
compileSdkVersion 27
minSdkVersion 21
// for using vectors for less than 21 API
vectorDrawables.useSupportLibrary = true
versionCode 56
dexOptions {
preDexLibraries true
javaMaxHeapSize "4g"
}
// Enabling multidex support.
multiDexEnabled true
versionName "1.6.5"
renderscriptTargetApi 2
renderscriptSupportModeEnabled true // Enable RS support
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
configurations {
all*.exclude group: 'commons-logging', module: 'commons-logging'
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
/********************************* Deep Linking Library *********************************************/
api 'com.airbnb:deeplinkdispatch:3.1.1'
annotationProcessor 'com.airbnb:deeplinkdispatch-processor:3.1.1'
/********************************* Android SDK *********************************************/
api "com.android.support:appcompat-v7:$support_library_version"
api 'com.android.support.constraint:constraint-layout:1.0.2'
api "com.android.support:percent:$support_library_version"
api "com.android.support:recyclerview-v7:$support_library_version"
api "com.android.support:gridlayout-v7:$support_library_version"
api "com.android.support:cardview-v7:$support_library_version"
api "com.android.support:design:$support_library_version"
api "com.android.support:support-vector-drawable:$support_library_version"
api "com.android.support:support-core-utils:$support_library_version"
api 'com.android.support:multidex:1.0.2'
}
低于我在布局编辑器中看到的内容:
下面我的xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.constraint.ConstraintLayout>
布局编辑器在这种情况下说:
如果我点击 显示异常,我将看到:
java.lang.NegativeArraySizeException
at android.support.constraint.R$styleable.<clinit>(Unknown Source)
at android.support.constraint.ConstraintLayout.init(ConstraintLayout.java:440)
at android.support.constraint.ConstraintLayout.<init>(ConstraintLayout.java:420)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:481)
at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:264)
at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:222)
at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:211)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:337)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:348)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:248)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:325)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:384)
at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:193)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:547)
at com.android.tools.idea.rendering.RenderTask.lambda$inflate(RenderTask.java:681)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
为了解决这个问题我尝试了很多但都没有成功。我没有使用简单的方法解决问题,例如使 cache/clean project/rebuild 项目无效等...
问题:
如果在 attr.xml 中定义的一个 <declare-stylable>
标签中有 太多 属性,布局编辑器似乎会遇到一些麻烦!
解法:
我在一个 <declare-stylable>
标签中有 49 个属性。
如果我删除 4 属性,那么我有 45,布局编辑器正在工作。
我还在 Google 问题跟踪器上添加了一个问题:
https://issuetracker.google.com/issues/71641021
更新自 Google 问题:
此问题已在 Android Studio 3.1 版本中修复。
不要使用 ConstraintLayout
,而是使用 Relative...
或 Linear...
。那应该可以。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical">
<EditText
android:layout_width="match_parent" />
</LinearLayout>