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

use*_*114 15 android

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

// 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>
Run Code Online (Sandbox Code Playgroud)

我认为两者都不可能吗?

谢谢

Vij*_*y C 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>
Run Code Online (Sandbox Code Playgroud)

RES /布局/ 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>
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助..


God*_*hen 1

想不出任何方法可以在 XML 中实现这一点。

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

http://developer.android.com/reference/android/view/View.html#onDraw(android.graphics.Canvas )