矩形形状的可绘制对象,如何指定顶部和底部的边框颜色?

16

有没有办法为渐变定义顶部和底部的描边,或者创建一个复合形状可绘制对象?

// option1:
<shape
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">

  <gradient
    android:startColor="#f00"
    android:endColor="#0f0"
    />
  <stroke
    top=1dip, yellow
    bottom=1dip, purple
    />
</shape>

// option2
<shape
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">

  <shape
    android:shape="rectangle"
    height=1dip, color=yellow />

  <gradient
    android:startColor="#f00"
    android:endColor="#0f0"
    />

  <shape
    android:shape="rectangle"
    height=1dip, color=purple />
</shape>

我认为两者都不可能实现?

谢谢


请参考以下链接:https://dev59.com/HnI-5IYBdhLWcg3w9tdw - Kenumir
4个回答

10

我猜可以使用layer-list来完成。

以下是res/drawable/layer_list_shape.xml
我已经使用硬编码的尺寸。

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
    <shape android:shape="rectangle">
        <size android:width="150dp" android:height="6dp"/>
        <solid android:color="#ff0" />
    </shape>
</item>
<item android:top="6dp">
    <shape android:shape="rectangle" >
        <size android:width="150dp" android:height="50dp"/>
        <gradient
            android:endColor="#0f0"
            android:startColor="#f00" />
    </shape>
</item>
<item android:top="82dp">
    <shape android:shape="rectangle" >
        <size android:width="150dp" android:height="6dp"/>
        <solid android:color="#ff0" />
    </shape>
</item>

res/layout/main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/layer_example" />
 </LinearLayout>

希望这可以帮助到你。


1

1
建立在Vijay C给出的答案上,您可以创建可绘制对象并将其包含在任何布局中。之前的答案还可以,但它添加了硬编码的尺寸,这使得它无法作为wrap_content用于背景。此外,这种方法可以将项目数量从3个减少到2个。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#d8dae3" />
        </shape>
    </item>
    <item
        android:bottom="2dp"
        android:top="2dp">
        <shape android:shape="rectangle">
            <gradient
                android:endColor="@android:color/white"
                android:startColor="@android:color/white" />
        </shape>
    </item>
</layer-list>

0

如果您不想使用硬编码的尺寸,可以使用3个视图:

<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"
android:background="@android:color/black" >

<View 
    android:layout_width="match_parent"
    android:layout_height="1dip" 
    android:background="@android:color/white"/>

<TextView
    android:id="@+id/header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/header" />

<View 
    android:layout_width="match_parent"
    android:layout_height="1dip" 
    android:background="@android:color/white"/>

</LinearLayout>

标题元素具有渐变背景。当然,您也可以使用任何其他布局。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接