2010-09-09 104 views
15

有什麼辦法來定義一個梯度的頂部和底部的行程,或創建一個複合形狀繪製?:可繪製矩形形狀,指定頂部和底部筆觸顏色?

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

我不認爲要麼是可能的嗎?

謝謝

+0

看到這一點:http://stackoverflow.com/questions/1598119/is-there-an-easy-way-to-add-一個邊界到頂部和底部的一個Android視圖 – Kenumir 2013-08-13 07:01:45

回答

10

我想,它可以通過使用layer-list完成。

以下是RES /繪製/ 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 /佈局/ 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> 

希望這會有所幫助..

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> 

頭部元素有一個漸變背景。當然,你也可以使用其他的佈局。

1

根據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> 
+0

謝謝,它有幫助。 – hue23289 2017-09-12 06:07:46

相關問題