简体   繁体   English

为什么 Android drawable 不显示为列表项背景?

[英]Why isn't Android drawable showing up as list item background?

I cannot figure out why the drawable I'm using is not showing up as the background, with border, for list items.我不明白为什么我使用的 drawable 没有显示为背景,带边框,用于列表项。 Here is the drawable in a file named ticketborder.xml:这是一个名为 ticketborder.xml 的文件中的 drawable:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#ff00ff00"/>
    <stroke android:width="4dp" android:color="#ffff0000"/>
</shape>

In the designer, I see a rendering of this.在设计器中,我看到了这样的渲染。 It looks correct.它看起来是正确的。 I also use this as the background in the XML for the layout for the items and it looks good there.我还使用它作为 XML 中项目布局的背景,它在那里看起来不错。

In the getView() function in the adapter, I tried to get around any XML issues and have this line:在适配器中的 getView() 函数中,我试图绕过任何 XML 问题并使用以下代码:

v.setBackground( ContextCompat.getDrawable(context, R.drawable.ticketborder ) );

This does nothing and the border doesn't show up and neither does the background color.这什么都不做,边框不显示,背景颜色也不显示。 To test that the code can even set the background of this view, I did this:为了测试代码甚至可以设置此视图的背景,我这样做了:

v.setBackgroundColor( Color.RED );

This works just fine.这工作得很好。 Somehow, it is only the drawable that won't work.不知何故,只有 drawable 不起作用。 Or maybe it's something to do with this being a list item that gets used when the getView() function is calledin the adapter.或者,这可能与这是在适配器中调用 getView() 函数时使用的列表项有关。 I know the drawable is valid because the designer shows it.我知道 drawable 是有效的,因为设计师展示了它。 The designer also shows a preview of the border and background in the layout for the list items.设计器还会在列表项的布局中显示边框和背景的预览。 Here is the layout for the list item that uses the drawable:这是使用 drawable 的列表项的布局:

?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.LinearLayoutCompat
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:clickable="false"
    android:padding="44dp"
    android:descendantFocusability="blocksDescendants"
    android:background="@drawable/ticketborder">
    ...

One more thing I should mention is that when I wrote the code for this a few months ago, the borders were showing up fine.我还应该提到的一件事是,当我几个月前为此编写代码时,边框显示良好。 The only thing that happened that I can recall is an update to Android Studio.我能记得的唯一一件事是对 Android Studio 的更新。 I'm sure I screwed up some code in the past few months but I can't for the life of me figure out how I could do anything that would make the background drawable not work right while a simple color works just fine.我确信在过去的几个月里我搞砸了一些代码,但我终其一生都无法弄清楚如何做任何会使背景可绘制的事情无法正常工作,而简单的颜色却能正常工作。

Here is a shot of the designer when I'm working on the drawable:这是设计师在我处理 drawable 时的照片:

在此处输入图片说明

Here is a picture of the design view when I am editing the layout of the list item:这是我编辑列表项布局时设计视图的图片:

在此处输入图片说明

And finally, a picture of the screen on the simulator (a physical phone has the same behavior) when using the red background color:最后,当使用红色背景颜色时,模拟器上的屏幕图片(物理手机具有相同的行为):

在此处输入图片说明

and using the drawable:并使用drawable:

在此处输入图片说明

EDIT:编辑:

This worked but I don't know why and I am not sure how to modify the color and border color in code:这有效,但我不知道为什么,我不确定如何修改代码中的颜色和边框颜色:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
            <solid android:color="#00ff00"/>
            <stroke android:width="2dp" android:color="#ff0000"/>
        </shape>
    </item>
</layer-list>

I have no explanation as to why this is working and the previous shape definition did not work.我无法解释为什么这会起作用,而之前的形状定义不起作用。 I am fairly certain that I have other LinearLayout elements that have shapes because I have a few places where I add rounded corners on borders around UI elements.我相当确定我还有其他具有形状的 LinearLayout 元素,因为我有几个地方在 UI 元素周围的边框上添加了圆角。 I'll need to check to see if those stopped working :(我需要检查它们是否停止工作:(

I fixed it by adding a layer to the XML for the background and border shape:我通过为背景和边框形状的 XML 添加一个图层来修复它:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
            <solid android:color="#00ff00"/>
            <stroke android:width="2dp" android:color="#ff0000"/>
        </shape>
    </item>
</layer-list>

I am not at all happy with this since I need to get the background of the element in code and alter the colors to fit with what a server is delivering to this client app.我对此一点也不满意,因为我需要在代码中获取元素的背景并更改颜色以适应服务器向该客户端应用程序提供的内容。 I also still want an explanation about why this works.我还想解释一下为什么会这样。

Note that I did try something like this initially so I'm also not sure how I got it wrong the first time I tried it.请注意,我最初确实尝试过类似的操作,因此我也不确定第一次尝试时我是如何出错的。 But this does work.但这确实有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM