【问题标题】:Android border bottom for custom view not working自定义视图的Android边框底部不起作用
【发布时间】:2015-05-16 19:22:25
【问题描述】:

我想要做的是有一个相对布局,上面有一个渐变和一个灰色的底部边框,我在我的应用程序中使用下面的自定义视图:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item_container"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<ImageView 
    android:id="@+id/list_item_image"
    android:layout_width="match_parent"
    android:layout_height="@dimen/SIZE_LIST_ITEM_LARGE"
    android:scaleType="centerCrop"/>

<View
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/shape_list_item_gradient" />

<com.rahil.widgets.CustomTextView 
    android:id="@+id/list_item_title"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:gravity="bottom"

    android:textColor="@color/COLOR_BLACK"/>

</RelativeLayout>

这是 shape_list_item_gradient:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
        <gradient
            android:startColor="#FFFFFFFF"
            android:endColor="#00FFFFFF"
            android:angle="90" />
    </shape>
</item>
<item android:bottom="2dp">
    <shape android:shape="rectangle">
        <stroke android:width="2dp" android:color="@color/COLOR_GRAY" />
    </shape>
</item> 
</layer-list>

但上面的代码将边框应用于所有边框,而不仅仅是底部边框。我将单独的形状 xml 添加到 relativelayout 但它不起作用,我假设带有渐变的视图可能会覆盖它?我安静地迷路了。感谢您的帮助。

【问题讨论】:

    标签: android android-layout android-relativelayout


    【解决方案1】:

    android:bottom 属性实际上是here 关于layer-list 可绘制对象的“底部偏移量”。没有明确的方法可以创建仅在一侧描边的矩形。

    但是,您可以使用 Krylez 在此处建议的相同技巧:https://stackoverflow.com/a/19239478/1538674

    所以是这样的:

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape android:shape="rectangle">
                <gradient
                    android:startColor="#FFFFFFFF"
                    android:endColor="#00FFFFFF"
                    android:angle="90" />
            </shape>
        </item>
        <item android:top="-3dp" android:right="-3dp" android:left="-3dp">
            <shape>
                <solid android:color="@android:color/transparent" />
                <stroke android:width="2dp" android:color="@color/COLOR_GRAY" />
            </shape>
        </item>
    </layer-list>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-19
      • 1970-01-01
      • 1970-01-01
      • 2020-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多