Android 矩形形状可绘制,指定顶部和底部笔划颜色?

Android 矩形形状可绘制,指定顶部和底部笔划颜色?,android,Android,有没有办法定义渐变的顶部和底部笔划,或者创建可绘制的复合形状 // option1: <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradient android:startColor="#f00" android:endColor="#0f0" /> <stroke top

有没有办法定义渐变的顶部和底部笔划,或者创建可绘制的复合形状

// 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>
//选项1:
//选择2
我认为两者都不可能


谢谢

我想不出任何方法在XML中实现这一点

但是你总是可以选择自己画画


)

我想,可以使用

以下是res/drawable/layer\u list\u 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>


希望这有帮助。

如果您不想使用硬编码尺寸,可以使用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>


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

在Vijay C给出的答案的基础上,您可以创建一个可绘制并包含在任何布局中。前面的答案是可以的,但它增加了硬编码的维度,这使得它不可能在后台用作包装内容。此外,这样可以将项目数从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>

请参见以下内容: