2010-09-09 72 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

相关问题