TextView不允许使用其中的位图绘制图层列表背景

时间:2019-01-25 20:05:49

标签: android xml bitmap background drawable

好吧,我似乎在无处不在进行了搜索和搜索,但找不到答案。我认为这是因为我试图一次做太多事情。我想多次使用带有位图的矩形背景图像,并使用来自TextView的不同文本。

ideal 这是我一直尝试使用的位图(.png)文件的示例:dice_one

我想要一个TextView,其背景为可绘制的XML文件,该文件是一个图层列表。查看代码:

box_ones xml:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:width="128dp" android:height="34dp">
            <shape android:shape="rectangle">
                <stroke android:width="1dp"/>
                <solid android:color="@color/ivory"/>
                <corners android:topLeftRadius="16dp" android:topRightRadius="16dp"/>
            </shape>
        </item>
        <item android:right="10dp">
            <bitmap android:src="@drawable/dice_one" android:gravity="right|center_vertical"/>
        </item>
    </layer-list>

box_twos xml

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:width="@string/tBoxWidth" android:height="@string/boxHeight">
            <shape android:shape="rectangle">
                <stroke android:width="1dp"/>
                <solid android:color="@color/ivory"/>
            </shape>
        </item>

<item android:right="10dp">
    <bitmap android:src="@drawable/dice_two" android:gravity="right|center_vertical"/>
</item>

activity_main xml:

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ones"
        android:background="@drawable/box_ones"
        android:textAlignment="gravity"
        android:gravity="center_vertical"
        android:textSize="16sp"
        />

字符串xml

<resources>
    <string name="xSize">16dp</string>
    <string name="boxWidth">40dp</string>
    <string name="boxHeight">34dp</string>
    <string name="tBoxWidth">128dp</string>
</resources>

我已经尝试了以上内容;我尝试做一个扩展TextView的自定义类;我试过做一个TextView和ImageView;我尝试做android:drawableEnd。但是,每种方法都会出现一些错误,使我无法使用所述方法。我在做什么错??

2019-01-25 14:24:29.978 4346-4346/com.dyna.ks.yscorepadv E/AndroidRuntime: FATAL EXCEPTION: main Process: com.dyna.ks.yscorepadv, PID: 4346 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dyna.ks.yscorepadv/com.dyna.ks.yscorepadv.FullscreenActivity}: android.view.InflateException: Binary XML file line #47: Binary XML file line #47: Error inflating class TextView at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2955) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6938) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) Caused by: android.view.InflateException: Binary XML file line #47: Binary XML file line #47: Error inflating class TextView Caused by: android.view.InflateException: Binary XML file line #47: Error inflating class TextView Caused by: android.content.res.Resources$NotFoundException: Drawable com.dyna.ks.yscorepadv:drawable/box_twos with resource ID #0x7f06005b Caused by: android.content.res.Resources$NotFoundException: File res/drawable/box_twos.xml from drawable resource ID #0x7f06005b at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:837) at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:659) at android.content.res.Resources.loadDrawable(Resources.java:900) at android.content.res.TypedArray.getDrawableForDensity(TypedArray.java:953) at android.content.res.TypedArray.getDrawable(TypedArray.java:928) at android.widget.TextView.<init>(TextView.java:1391) at android.widget.TextView.<init>(TextView.java:1129) at androidx.appcompat.widget.AppCompatTextView.<init>(AppCompatTextView.java:87) at androidx.appcompat.widget.AppCompatTextView.<init>(AppCompatTextView.java:83) at androidx.appcompat.app.AppCompatViewInflater.createTextView(AppCompatViewInflater.java:177) at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:102) at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1266) at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1316) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:772) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730) at android.view.LayoutInflater.rInflate(LayoutInflater.java:863) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) at android.view.LayoutInflater.rInflate(LayoutInflater.java:866) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) at android.view.LayoutInflater.inflate(LayoutInflater.java:515) at android.view.LayoutInflater.inflate(LayoutInflater.java:423) at android.view.LayoutInflater.inflate(LayoutInflater.java:374) at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469) at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) at com.dyna.ks.yscorepadv.FullscreenActivity.onCreate(FullscreenActivity.java:13) at android.app.Activity.performCreate(Activity.java:7183) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2908) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6938) at java.lang.reflect.Method.invoke(Native Method) 2019-01-25 14:24:29.979 4346-4346/com.dyna.ks.yscorepadv E/AndroidRuntime: at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) Caused by: java.lang.UnsupportedOperationException: Can't convert value at index 2 to dimension: type=0x3 at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:730) at android.graphics.drawable.LayerDrawable.updateLayerFromTypedArray(LayerDrawable.java:376) at android.graphics.drawable.LayerDrawable.inflateLayers(LayerDrawable.java:266) at android.graphics.drawable.LayerDrawable.inflate(LayerDrawable.java:194) at android.graphics.drawable.DrawableInflater.inflateFromXmlForDensity(DrawableInflater.java:146) at android.graphics.drawable.Drawable.createFromXmlInnerForDensity(Drawable.java:1322) at android.graphics.drawable.Drawable.createFromXmlForDensity(Drawable.java:1281) at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:824) ... 36 more

1 个答案:

答案 0 :(得分:-1)

显然,这是一个SDK问题。我的最低SDK为19。为了在可绘制对象中使用android:widthandroid:height,我的最低SDK必须为23。

enter image description here