插入不可用不如文档中所述

时间:2014-11-10 18:25:03

标签: android android-layout android-drawable

如Android开发者网站上所述:

  

以XML格式定义的drawable,它将指定的另一个drawable包含在内   距离。当View需要背景时,这非常有用   小于View的实际界限。

http://developer.android.com/guide/topics/resources/drawable-resource.html#Inset

不仅仅是移动背景并将内容保留在我的情况下,插图也移动了TextView。

这是布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg_inset"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Some text"
            android:textSize="23sp" />

    </LinearLayout>

这是bg_inset drawable

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@android:color/darker_gray"
    android:insetLeft="100dp" />

这是我得到的结果:

Layout result

3 个答案:

答案 0 :(得分:13)

默认情况下,设置背景可绘制也会将可绘制的填充应用于视图。如果您不希望它与背景可绘制匹配,请明确设置填充。

答案 1 :(得分:2)

尝试将shape与left-padding一起使用,而不是inset

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="@android:color/darker_gray"/>
    <padding android:left="100dp"/>
</shape>

答案 2 :(得分:0)

另一个选择:如果您想使用DP而不是PX,请尝试以下我的建议。根据我的经验,InsetDrawable仅适用于像素,我觉得这无济于事。老实说,我对这种解决方案并不满意,但是,它可以工作。

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

<item
        android:bottom="10dp"
        android:drawable="@drawable/image_placeholder"
        android:top="10dp" />
</layer-list>
相关问题