【问题标题】:Problem with 9 patch image as background9个补丁图像作为背景的问题
【发布时间】:2017-09-07 14:28:41
【问题描述】:

我有一个列表视图结构,其相对布局使用 奇数/偶数元素的交替背景图像。我正在尝试设置 通过计算位置动态绘制的背景。它 与普通位图一起工作得很好。但是当我尝试使用 Ninepatch 图像破坏了 UI,所有元素都被扭曲了。什么 我做错了吗?是不是 Ninepatch 图像是如何创建的? 与普通图像相比,使用 Ninepatch 图像有不同的方法 位图。

我的列表视图 XML 是这样的

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content" android:id="@+id/id01"
       android:background="@drawable/my_9patch_bg_image">
       <ImageView  />
       <RelativeLayout>
         <ImageView  />
          <TextView  />
          <TextView  />
       </RelativeLayout>
</RelativeLayout>

可能是here 的解决方案可能适用于我的问题。虽然我必须尝试一下,但它是准确的。

【问题讨论】:

  • 可能是您的 9-patch 格式错误?
  • 我尝试了 9 个补丁图像的不同变体,所有这些都提供了我想要的预览。但是当特定的列表视图活动启动时,我的第一个 RelativeLayout 中的内容就消失了。不过可以正常使用普通位图图像。
  • 正在发生的事情的屏幕截图、您想要发生的事情的图像以及 9 补丁可能会帮助人们确定问题所在。
  • 这里 stackoverflow.com/questions/3904852/… 与代码示例有同样的问题 :)

标签: android listview nine-patch


【解决方案1】:

自从一年来没有人回答你... 我遇到了同样的问题,您需要将填充设置为零(即使您不需要它)。

祝你好运。

【讨论】:

  • 对我不起作用,9 补丁仍然显示拉伸。
【解决方案2】:

您可以像使用普通可绘制对象一样使用九个补丁。我假设您对 9 补丁本身有问题。在你的 android sdk 的 /tools 目录中,你有 draw 9 补丁工具。它将帮助您更正 9 个补丁可绘制对象并针对不同大小的视图进行预览。

【讨论】:

  • 我尝试使用 draw9patch 工具修复图像。尝试了不同的变化,但没有得到它的工作。如果图像应该在 (+x,-y) 方向上缩放,那么理想的绘制方式应该是什么?
【解决方案3】:

选择除角以外的所有边作为拉伸边

【讨论】:

    【解决方案4】:

    您可以使用此 xml 作为背景以显示为卡片视图。

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:dither="true" android:shape="rectangle">
            <corners android:radius="2dp" />
            <solid android:color="#e2e2e2" />
        </shape>
    </item>
    <item android:bottom="2dp">
        <shape android:dither="true" android:shape="rectangle">
            <corners android:radius="2dp" />
            <solid android:color="@color/white" />
        </shape>
    </item>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-22
      • 2016-07-27
      相关资源
      最近更新 更多