我在一个片段上添加了两个浮动操作按钮,但总是出现以下错误:
无效的可绘制对象已添加到LayerDrawable中! Drawable已经属于另一个所有者,但是没有公开恒定状态
<RelativeLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="30dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_weight="1"
android:id="@+id/scrollView"
android:layout_marginRight="30dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout_main_account">
<TextView
android:text="Name"
android:textStyle="bold"
android:layout_marginTop="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_activity_main_account_name_label"/>
<EditText
android:enabled="false"
android:inputType="none"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_activity_main_account_name_label_value"/>
</LinearLayout>
</ScrollView>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="0dp"/>
<android.support.design.widget.FloatingActionButton
android:backgroundTint="#000"
android:layout_marginBottom="5dp"
android:layout_marginRight="10dp"
app:layout_anchorGravity="bottom"
android:layout_width="wrap_content"
android:layout_gravity="bottom|end"
android:layout_height="wrap_content"
android:src="@drawable/ic_edit_account"
android:id="@+id/fbtn_activity_main_account_edit_account"/>
<android.support.design.widget.FloatingActionButton
android:backgroundTint="#000"
android:layout_marginBottom="5dp"
app:layout_anchorGravity="bottom"
android:layout_width="wrap_content"
android:layout_gravity="bottom|end"
android:layout_height="wrap_content"
android:src="@drawable/ic_home_account"
android:id="@+id/fbtn_activity_main_account_manage_addresses"
app:layout_anchor="@id/fbtn_activity_main_account_edit_account"/>
</LinearLayout>
</RelativeLayout>
可绘制-> ic_edit_account.xml
<vector android:height="24dp" android:viewportHeight="60.017"
android:viewportWidth="60.017" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FFFFFF" android:pathData="M59.144,3.731l-2.85,-2.851c-1.164,-1.161 -3.057,-1.162 -4.221,0.001l-3.126,3.126H0v56h56V11.097l0.305,-0.305l0,0l2.839,-2.839C60.308,6.789 60.308,4.895 59.144,3.731zM20.047,36.759l3.22,3.22l-4.428,1.208L20.047,36.759zM52.062,12.206L47.82,7.964l1.414,-1.414l4.243,4.242L52.062,12.206zM50.648,13.62L25.192,39.076l-4.242,-4.242L46.406,9.378L50.648,13.62zM54,13.097v44.91H2v-52h44.947L18.829,34.127l-0.188,0.188l-2.121,7.779l-1.226,1.226c-0.391,0.391 -0.391,1.023 0,1.414c0.195,0.195 0.451,0.293 0.707,0.293s0.512,-0.098 0.707,-0.293l1.226,-1.226l7.779,-2.123l26.351,-26.35h0l0.447,-0.447L54,13.097zM57.73,6.539l-2.839,2.839l-4.243,-4.243l2.839,-2.839c0.384,-0.384 1.009,-0.383 1.393,0l2.85,2.85C58.114,5.529 58.114,6.155 57.73,6.539z"/>
</vector>
可绘制-> ic_home_account.xml
<vector android:height="24dp" android:viewportHeight="512"
android:viewportWidth="512" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FFFFFFFF" android:pathData="M506.555,208.064L263.859,30.367c-4.68,-3.426 -11.038,-3.426 -15.716,0L5.445,208.064c-5.928,4.341 -7.216,12.665 -2.875,18.593s12.666,7.214 18.593,2.875L256,57.588l234.837,171.943c2.368,1.735 5.12,2.57 7.848,2.57c4.096,0 8.138,-1.885 10.744,-5.445C513.771,220.729 512.483,212.405 506.555,208.064z"/>
<path android:fillColor="#FFFFFFFF" android:pathData="M442.246,232.543c-7.346,0 -13.303,5.956 -13.303,13.303v211.749H322.521V342.009c0,-36.68 -29.842,-66.52 -66.52,-66.52s-66.52,29.842 -66.52,66.52v115.587H83.058V245.847c0,-7.347 -5.957,-13.303 -13.303,-13.303s-13.303,5.956 -13.303,13.303v225.053c0,7.347 5.957,13.303 13.303,13.303h133.029c6.996,0 12.721,-5.405 13.251,-12.267c0.032,-0.311 0.052,-0.651 0.052,-1.036v-128.89c0,-22.009 17.905,-39.914 39.914,-39.914s39.914,17.906 39.914,39.914v128.89c0,0.383 0.02,0.717 0.052,1.024c0.524,6.867 6.251,12.279 13.251,12.279h133.029c7.347,0 13.303,-5.956 13.303,-13.303V245.847C455.549,238.499 449.593,232.543 442.246,232.543z"/>
</vector>
build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.atta"
minSdkVersion 23
targetSdkVersion 26
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:26.1.0'
implementation 'com.android.support:design:26.1.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'
// retrofit, gson
implementation 'com.google.code.gson:gson:2.6.2'
implementation 'com.squareup.okhttp3:okhttp:3.8.0'
implementation 'com.squareup.retrofit2:retrofit:2.0.2'
implementation 'com.basgeekball:awesome-validation:4.1'
implementation 'com.squareup.retrofit2:converter-gson:2.0.2'
implementation 'com.squareup.okhttp3:logging-interceptor:3.8.0'
}
注意:每个可绘制对象仅使用一次。
以下线程无法解决我的问题:
Drawable already belongs to another owner but does not expose a constant state
发现问题:以下属性导致了该异常:
android:backgroundTint =“#000”
如果我不使用它,FAB上会有粉红色的背景:(
答案 0 :(得分:4)
已修复:
在顶部布局中添加了 xmlns:app =“ http://schemas.android.com/apk/res-auto”
使用 app:backgroundTint =“ @ color / colorFABTint” 代替 android:backgroundTint =“#000”
在 colors.xml
中添加了以下行<color name="colorFABTint">#000000</color>
结果xml:
<RelativeLayout
android:layout_margin="30dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.design.widget.FloatingActionButton
android:layout_marginBottom="5dp"
app:layout_anchorGravity="bottom"
android:layout_width="wrap_content"
android:layout_gravity="bottom|end"
android:layout_height="wrap_content"
android:src="@drawable/ic_home_account"
app:backgroundTint="@color/colorFABTint"
android:id="@+id/fbtn_activity_main_account_manage_addresses"
app:layout_anchor="@id/fbtn_activity_main_account_edit_account"/>
答案 1 :(得分:0)
Drawable有一个状态,如果您将它分配给多个浮动操作按钮,那么跟踪Drawable的状态就会出现问题。
也许有更好的解决方案,但是一个解决方案可能是在onCreate
中设置工厂图标而不是布局,您可以从布局中删除工厂src
并使用以下方法:
fab.setImageResource(getResources().getDrawable(R.drawable.ic_edit_account).mutate());
或者也许是这个
fab.setImageResource(getResources().getDrawable(R.drawable.ic_edit_account).getConstantState().newDrawable());