在您的android主题中创建和使用android drawable引用

时间:2018-07-13 19:30:27

标签: android android-xml xml-drawable

我无法以自己的样式创建可绘制参考 我的styles.xml文件看起来像这样

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="myDark" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/lightColorPrimary</item>
    <item name="colorPrimaryDark">@color/lightColorPrimaryDark</item>
    <item name="colorAccent">@color/lightColorAccent</item>
    <item name="android:windowTitleSize">@dimen/titleSize</item>
    <item name="windowNoTitle">true</item>
    <item name="msgreceivedbg">@drawable/mrrd</item>
</style>
<style name="myLight" parent="Theme.AppCompat.DayNight.DarkActionBar">
    <item name="colorPrimary">@color/darkColorPrimary</item>
    <item name="colorPrimaryDark">@color/darkColorPrimaryDark</item>
    <item name="colorAccent">@color/darkColorAccent</item>
    <item name="android:windowTitleSize">@dimen/titleSize</item>
    <item name="windowNoTitle">true</item>
    <item name="msgreceivedbg">@drawable/mrrl</item>
</style>
</resources>

我的attrs.xml文件看起来像

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="msgreceivedbg" format="reference" />
</resources>

当我尝试在布局文件中使用此属性时,出现以下错误

--------- beginning of crash
07-13 08:48:08.893 9913-9913/com.messaging.chatify E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.messaging.chatify, PID: 9913
android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class <unknown>
Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class <unknown>
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.newInstance0(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
    at android.view.LayoutInflater.createView(LayoutInflater.java:645)
    at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:717)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:785)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
    at com.messaging.chatify.Adapters.MessagesAdapter.onCreateViewHolder(MessagesAdapter.java:125)
    at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6493)
    at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5680)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5563)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5559)
    at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2229)
    at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1556)
    at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516)
    at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:608)
    at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3693)
    at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3410)
    at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3962)
    at android.view.View.layout(View.java:17637)
    at android.view.ViewGroup.layout(ViewGroup.java:5575)
    at android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1855)
    at android.view.View.layout(View.java:17637)
    at android.view.ViewGroup.layout(ViewGroup.java:5575)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:17637)
    at android.view.ViewGroup.layout(ViewGroup.java:5575)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
    at android.view.View.layout(View.java:17637)
    at android.view.ViewGroup.layout(ViewGroup.java:5575)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:17637)
    at android.view.ViewGroup.layout(ViewGroup.java:5575)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
    at android.view.View.layout(View.java:17637)
    at android.view.ViewGroup.layout(ViewGroup.java:5575)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at com.android.internal.policy.DecorView.onLayout(DecorView.java:726)
    at android.view.View.layout(View.java:17637)
    at android.view.ViewGroup.layout(ViewGroup.java:5575)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2346)
    07-13 08:48:08.894 9913-9913/com.messaging.chatify E/AndroidRuntime:     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2068)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
    at android.view.Choreographer.doCallbacks(Choreographer.java:686)
    at android.view.Choreographer.doFrame(Choreographer.java:621)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
 Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 13: TypedValue{t=0x2/d=0x7f040112 a=-1}
    at android.content.res.TypedArray.getDrawable(TypedArray.java:925)
    at android.view.View.<init>(View.java:4202)
    at android.widget.TextView.<init>(TextView.java:710)
    at android.widget.TextView.<init>(TextView.java:704)
    at android.widget.TextView.<init>(TextView.java:700)
        ... 66 more

我正在使用一个回收站视图,在该视图中将使用textView的视图充气到下面一行中

android:background="?attr/msgreceivedbg"

可绘制的mrrd和mrrl具有与的色差

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/darkColorPrimary" />
            <corners android:radius="5dp" />
        </shape>
    </item>
</selector>

这两个可绘制对象只是android:color属性不同...

我正在android 7.1.1 genymotion自定义手机上对其进行测试

布局文件是这个

<?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="wrap_content"
android:paddingTop="8dp">

<TextView
    android:id="@+id/text_message_body6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:background="?attr/msgreceivedbg"
    android:maxWidth="240dp"
    android:padding="8dp"
    android:text="hello, hello!"
    android:textColor="@android:color/white"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    tools:ignore="HardcodedText" />

<TextView
    android:id="@+id/text_message_time6"
    android:text="11:40"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="10sp"
    android:background="?attr/msgreceivedbg"
    android:textColor="@android:color/white"
    android:layout_marginStart="4dp"
    app:layout_constraintBottom_toBottomOf="@id/text_message_body6"
    app:layout_constraintLeft_toRightOf="@id/text_message_body6"
    tools:ignore="HardcodedText" />

</android.support.constraint.ConstraintLayout>

任何帮助将不胜感激...

1 个答案:

答案 0 :(得分:0)

更新:

根据评论,这是更新的答案。

在清单xml文件中,您必须使用与“回收者”视图相同的主题(除非您明确为“回收者”视图设置主题)

AndroidManifest.xml

 <activity   android:theme="@style/myDark" 
            android:name=".YOUR_ACTIVITY />

YOUR_ACTIVITY 是实施回收者视图的活动。

如果您为该活动使用其他主题,则必须在回收者视图中明确设置主题,如下所示,

<android.support.v7.widget.RecyclerView
        android:theme="@style/myDark"
        android:id="@+id/rv1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager">

    </android.support.v7.widget.RecyclerView>