【问题标题】:layer list in android not displaying images properlyandroid中的图层列表无法正确显示图像
【发布时间】:2017-10-11 05:58:47
【问题描述】:

我必须做出这样的形状-

为此,我编写了这样的代码-

三角形.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <rotate
            android:fromDegrees="45"
            android:pivotX="-45%"
            android:pivotY="97%"
            android:toDegrees="45" >
            <shape android:shape="rectangle" >
                <solid android:color="@color/nypd_blue" />
            </shape>
        </rotate>
    </item>

</layer-list>

和这样的矩形-

<?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:id="@+id/listview_background_shape">
            <stroke android:width="2dp" android:color="@color/nypd_blue" />
            <padding android:left="2dp"
                android:top="2dp"
                android:right="2dp"
                android:bottom="2dp" />

            <solid android:color="@color/nypd_blue" />
        </shape>
    </item>
</layer-list>

我把它们放在一个像这样的 xml 文件中-

背景.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/blue_triangle" android:id="@+id/triangle" android:gravity="top"
        android:top="0dp" android:bottom="0dp" android:left="0dp" android:right="0dp"
        />
    <item android:drawable="@drawable/blue_rectangle" android:gravity="bottom"
        android:height="10dp"
        />
</layer-list>

当我在我的代码中使用背景 .xml 来设置背景时,整个图像没有正确显示,只有矩形部分出现。这里有什么问题?

<LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
/>
<RelativeLayout android:id="@+id/hours_spent_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/width_1dp"
        android:background="@drawable/background">

【问题讨论】:

  • 您的图片链接似乎不可用。
  • 现在有货了吗?@LiJianixn
  • Http 错误:503
  • 我不知道为什么会来
  • 你可以把你的图片或截图放到stackoverflow网站上。

标签: android xml android-layout drawable xml-drawable


【解决方案1】:

您似乎在 API 低于 26 的设备上测试您的代码。事实上,我不知道如何解释为什么 LayerDrawable 在 API 26 下的行为不同。但我有一个解决方案。

 RelativeLayout relativeLayout;// your layout
 LayerDrawable drawable = (LayerDrawable) relativeLayout.getBackground();
 drawable.setLayerInset(1, padding, padding, padding, padding);

“1”表示索引1,在您的代码中“1”表示@drawable/blue_rectangle,您可以手动设置填充。

请注意,您应该将 dp 转换为像素。

【讨论】:

  • 为什么这里需要java代码?不能只使用drawable来完成吗?
  • layer-list.xml 将转换为 LayerDrawable。你是什​​么意思can't it be done by just using the drawable?
  • 有一种通过drawable设置padding jsut的方法。所以,我认为它也可以这样做
  • 我已经解释了当你在layer-list中设置填充时Android6.0以下有一个错误。为什么不试试我的解决方案?
  • 我一定会的!谢谢
猜你喜欢
  • 1970-01-01
  • 2020-01-07
  • 1970-01-01
  • 2020-08-27
  • 2021-09-26
  • 2018-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多