当我将9.PNG图像设置为RelativeLayout背景时,其中的所有元素都缩小到中间.这是XML和快照.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Just a simple white text which does not show up"
android:textColor="#ffffff"
/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

如果我将背景更改为颜色或普通图像,我会得到文本.
android:background="@drawable/bg2" //normal image
Run Code Online (Sandbox Code Playgroud)

谁能解释为什么会这样?
我无法深入了解android如何实现其layer-list drawable.但我发现它很有趣,我几乎不知道为什么会这样.
这里有一些drawables:
九补丁xml
<?xml version="1.0" encoding="utf-8"?>
<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/cam2tst_ripple_bg_img">
</nine-patch>
Run Code Online (Sandbox Code Playgroud)
形状xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<padding
android:left="@dimen/cam2tst_ripple_horizontal_padding"
android:top="@dimen/cam2tst_ripple_vertical_padding"
android:right="@dimen/cam2tst_ripple_horizontal_padding"
android:bottom="@dimen/cam2tst_ripple_vertical_padding" />
<solid android:color="@android:color/holo_green_dark" />
</shape>
Run Code Online (Sandbox Code Playgroud)
波纹xml
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@android:color/holo_green_light">
<item android:drawable="@drawable/cam2tst_ripple_shape"></item>
</ripple>
Run Code Online (Sandbox Code Playgroud)
包含以上所有内容的图层列表
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/cam2tst_ripple_bg_img" />
<item android:drawable="@drawable/cam2tst_ripple_base" />
</layer-list>
Run Code Online (Sandbox Code Playgroud)
不幸的是,我仍然无法在我的L预览中获得截图,但我可以对其进行描述.
我得到的是形状(我显然没有明确地设置它的大小)并没有覆盖整个九个补丁!九个补丁的未拉伸部分神奇地被认为是某种"自动填充物" ".我期待的是什么(好吧,我正在期待android为我做了什么,我的意思是我......假设...)是不是那么积极的东西:不是特别大小的形状可绘制覆盖整个九-patch就像后者是正常的png一样.
但是这个形状确实神奇地避免了9个补丁的未拉伸部分,并且只覆盖了九个补丁的拉伸部分.
这太棒了......但令人困惑,为什么?我可能无法深入挖掘源,但这听起来反直觉(但很好).我想知道原因.所以我在这里发布.
因为我将其标记为android-L,因为我正在开发一个.但我认为这应该是从像姜饼这样的东西.(只是用其他东西替换波纹可绘制,也许是插入抽屉等)
android drawable nine-patch android-layout android-5.0-lollipop
警告:这个问题中的XML是错误的,请在混淆自己之前阅读答案!
我一直在墙上撞了一会儿.以下帖子阐明了这一主题,但未能解决我的问题: Android ListView状态列表未通过自定义选择器显示默认项目背景和ListView项目背景
当我选择列表项时,正确的九补丁背景显示完美,但我无法获得最初显示的默认九补丁背景.在我看来,我需要以某种方式设置默认项目背景,但我无法弄清楚如何这样做.
列表显示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/shopListHeader"
/>
<ListView
android:id="@+id/shopList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dividerHeight="0px"
android:divider="#FFFFFFFF"
android:listSelector="@drawable/shop_list_selector"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
选择:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:visible="true">
<!-- the list items are enabled and being pressed -->
<item
android:state_pressed="true"
android:drawable="@drawable/shop_list_item_pressed" />
<item
android:state_selected="true"
android:textColor="#FFFFFFFF" />
</selector>
Run Code Online (Sandbox Code Playgroud)
背景:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:visible="true">
<item
android:state_selected="true"
android:drawable="@android:color/transparent" />
<item
android:drawable="@drawable/shop_list_item" />
</selector>
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我已经从参考文献中删除了示例.
您可能还注意到后台选择器未在任何位置引用.我开始在随机的地方扔它(如果应用程序编译添加要么没有效果或导致强制关闭)
当选择一个项目但没有按下时,我也试图阻止文本的颜色变为黑色和灰色(可以通过滚动列表来完成).由于我的背景在中心是黑色,因此选中时文本会变得部分不可见.据我所知,这个添加(选择器中的最后一个项目节点)什么都不做.
有没有人有任何想法让这个荒谬耗时的功能工作?
当我必须从完整图像(背景图像)创建九个补丁时,Android SDK工具draw9patch太慢了,因为它不允许从上到下快速绘制黑线(就像我们在Photoshop或Gimp中使用的那样),但我必须画每个像素.所以浪费时间.我想你知道我在说什么.
那么,有没有人试图在Photoshop/GIMP中绘制黑线,注意不要填充最终像素?Android SDK会将它们视为常规的9 PNG图像吗?
我有一个9-patch按钮,右侧为drawable,文本分配给按钮,当我使用普通图像显示文本时,当我使用它显示文本的颜色时,但是当我使用9补丁图像时它会不显示图像.
这是代码+图像;
<Button
android:id="@+id/btn_active_timer"
android:layout_width="300.00dp"
android:layout_height="38.00dp"
android:layout_marginLeft="10.00dp"
android:layout_above="@+id/gui_menu_container"
android:layout_marginBottom="6.00dp"
android:background="@drawable/ui_black"
android:drawableRight="@drawable/ui_arrow_small"
android:text="Whats the time mr woolf?"
android:textColor="#FFFFFF"
android:textSize="15.00dp"
android:gravity="left|center_vertical"
/>
Run Code Online (Sandbox Code Playgroud)

我用android工具draw9patch.bat做了一个9补丁图像.只在右侧画一条黑线,我希望它在那里伸展.保存为.9.png并将其设置为TextView的背景:
android:background="@drawable/myninepatch"
Run Code Online (Sandbox Code Playgroud)
它不起作用,我在应用程序中使用黑线获取图像,但它不会延伸.我究竟做错了什么?
我在一些地方看到除了伸缩区域的线条之外,在另一侧还有一个非常小的标记区域,但我不知道它是什么,我没有使用它.
我为背景区域创建了一个9patch,它似乎是拉伸的区域,我没有标记为拉伸.我有它,所以垂直它应该从中间拉伸1个像素,但它仍然拉伸顶部和底部边界.这是我的9patch,放在drawable文件夹中:

background以下是作为布局属性放置的结果图像:

为什么它会拉伸顶部和底部边框?我希望这些在顶部保持2个像素,在底部保持4个像素,但是它的拉伸.
如果您想尝试一下,可以在此处下载图像.重命名它,所以它有一个.9.png扩展,把它作为background一个View约100dp高大,采取截图,然后放大,你可以看到我所看到的.
开始了.我有一个非按下状态背景为透明的按钮.按下按钮后,背景不再透明(参见图片).我为你的所有图像使用九个补丁.
这是我的选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/starfighterstartbtndown" android:state_pressed="true"/>
<item android:drawable="@drawable/starfighterstartbtn"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
这是布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/mainMenuImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@drawable/starfighter" >
</ImageView>
<RelativeLayout
android:id="@+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="@android:color/transparent"
android:clickable="true"
android:hapticFeedbackEnabled="true"
android:src="@drawable/startselector" >
</ImageButton>
<ImageButton
android:id="@+id/btnExit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:clickable="true"
android:src="@drawable/exitselector"
android:background="@android:color/transparent"
android:hapticFeedbackEnabled="true" >
</ImageButton>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
未点击:

点击:

我也试过以下方法没有运气
button.getBackground().setAlpha(0);
button.setBackgroundColor(Color.TRANSPARENT);
Run Code Online (Sandbox Code Playgroud)
还在onClickListener()内部尝试了以下内容
view.getBackground().setAlpha(0);
view.setBackgroundColor(Color.TRANSPARENT);
button.getBackground().setAlpha(0);
button.setBackgroundColor(Color.TRANSPARENT);
Run Code Online (Sandbox Code Playgroud)
仍然没有运气.祝好运 :)
我创建了以下Nine-Patch PNG图像

但当我将其添加为按钮的背景时,我得到以下结果:

正如你可以看到图片没有填满所有按钮背景!
这是XML布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@drawable/ninepatchimage"
android:text="Click ME" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
谁能告诉我为什么?
我正面临一个奇怪的问题,试图将9个补丁drawables放入新的android studio mipmap目录.我已将所有图像放到指定的文件夹中,就像我以前使用drawable一样,但当我尝试将其包含在项目中时,它说
无法解析符号@mipmap/myfile
但它绝对"找到"它,你可以在屏幕截图中看到.

我认为它无法解析符号,因为"真实"文件名有.9,但点不能用于定义drawable.(我试过,它没用:))
有人esle面临同样的问题吗?目前,我已经添加了drawables文件夹并定义了eclipse-style的所有内容并且它正在工作,但这是解决问题的丑陋方式.谢谢.