我遇到的问题是我的drawable资源使用了tileMode repeat.在某些情况下,图像只是拉伸,有时会被正确重复.
以下是我用来创建按钮状态的xml文件:
用于重复拼贴的图像绘制

^^^^^^^^^^^^^
btn_menu_item.xml
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize="true" android:visible="true" android:variablePadding="true">
<!-- selected -->
<item
android:state_selected="true"
android:drawable="@drawable/menu_item_selected"/>
<!-- focused -->
<item
android:state_focused="true"
android:drawable="@drawable/menu_item_pressed"/>
<!-- pressed -->
<item
android:state_pressed="true"
android:drawable="@drawable/menu_item_pressed"/>
<!-- normal -->
<item
android:state_pressed="false"
android:state_focused="false"
android:drawable="@drawable/menu_item_normal"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
menu_item_normal.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_selected="true">
<shape android:shape="rectangle">
<gradient
android:startColor="#757575"
android:endColor="#929292"
android:angle="90"/>
</shape>
</item>
<item>
<bitmap
android:src="@drawable/menu_lines_texture"
android:tileMode="repeat"
android:dither="true"/>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
menu_item_pressed.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_selected="true">
<shape android:shape="rectangle">
<gradient
android:startColor="#dd4e00"
android:endColor="#c64600"
android:angle="90"/>
</shape>
</item>
<item>
<bitmap
android:src="@drawable/menu_lines_texture"
android:tileMode="repeat"
android:dither="true"/>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
请看下面我正在谈论的内容.

这是我的自定义选择器(StateListDrawable)
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/common_cell_background" />
<item
android:state_pressed="true"
android:drawable="@drawable/common_cell_background_highlight" />
<item
android:state_focused="true"
android:drawable="@drawable/common_cell_background_highlight" />
<item
android:state_selected="true"
android:drawable="@drawable/common_cell_background_highlight" />
</selector>
Run Code Online (Sandbox Code Playgroud)
common_cell_background和common_cell_background_highlight都是XML.代码如下:
common_cell_background.xml
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/common_cell_background_bitmap"
android:tileMode="repeat"
android:dither="true">
</bitmap>
Run Code Online (Sandbox Code Playgroud)
common_cell_background_highlight.xml
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/common_cell_background_bitmap_highlight"
android:tileMode="repeat"
android:dither="true">
</bitmap>
Run Code Online (Sandbox Code Playgroud)
位图也完全相同.亮点只是稍微轻一点,没有其他差异.两个位图都是PNG文件.
现在我开始了
convertView.setBackgroundResource(R.drawable.list_item_background);
Run Code Online (Sandbox Code Playgroud)
这是问题所在.我的common_cell_background没有重复,它已经拉长了.但是,当我触摸列表背景的单元格时,令人惊讶的是变为common_cell_background_highlight并猜测是什么?一切都很好,它应该像它应该重复.我不知道问题出在哪里,为什么我的背景不会重复突出显示.有什么想法吗?