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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3680211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:19:21  来源:igfitidea点击:

Rectangle shape drawable, specify top and bottom stroke colors?

android

提问by user246114

Is there any way to define a top and bottom stroke for a gradient, or create a compound shape drawable?:

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

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

I don't think either are possible?

我认为两者都不可能?

Thanks

谢谢

回答by Vijay C

I guess, it can be done by using layer-list.

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

Following is the res/drawable/layer_list_shape.xml
I have used hard-coded dimensions..

以下是我使用硬编码尺寸的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>

res/layout/main.xml

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>

Hope this helps..

希望这可以帮助..

回答by Goddchen

Can't think of any way to make this possible in XML.

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

But you always have the option to do the drawing on your own...

但是您始终可以选择自己进行绘图...

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

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

回答by Ankur Jain

building up on the answer given by Vijay C, you can create a drawable and include in any layout. The previous answer was ok but it added hard-coded dimensions which made it impossible to be used in background as wrap_content. Also, this way you reduce the number of items from 3 to 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>

回答by malimo

If you don't want to use hardcoded dimensions it's possible to use 3 views:

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

The header element has a gradient background. Of course, you could use any other layout as well.

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