Выравнивание прямоугольной формы, укажите цвета верхнего и нижнего штрихов?

Есть ли способ определить верхний и нижний штрих для градиента или создать составную форму для рисования?:

// 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

Я думаю, это можно сделать, используя 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>

Рез/макет/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>

Надеюсь, что это поможет.

Ответ 2

основываясь на ответе, заданном Vijay C, вы можете создать drawable и включить в любой макет. Предыдущий ответ был в порядке, но он добавил жестко закодированные размеры, которые сделали невозможным использование в фоновом режиме как 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>

Ответ 4

Если вы не хотите использовать жестко установленные размеры, вы можете использовать 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>

Элемент заголовка имеет фон градиента. Конечно, вы могли бы использовать любой другой макет.