【问题标题】:Android rectangle shape with two different color具有两种不同颜色的Android矩形形状
【发布时间】:2016-09-30 07:26:22
【问题描述】:

如何通过使用两种不同颜色的阴影来创建矩形形状?如上图。

【问题讨论】:

  • 使用<layer-list>

标签: android xml shape


【解决方案1】:
Please create a drawable file and put the below code in it.

    <?xml version="1.0" encoding="utf-8"?>
        <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
            <item>
                <shape android:shape="rectangle" >
                    <size android:height="20dp" />
                    <solid android:color="#F66D08" />
                </shape>
            </item>
            <item android:top="50dp">
                <shape android:shape="rectangle" >
                    <gradient android:endColor="#AD1B1D"
                        android:startColor="#E2360A"
                        android:angle="270" />
                </shape>
            </item>
        </layer-list>

【讨论】:

    【解决方案2】:

    layer-list 可以用来解决这个问题

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item>
            <shape android:shape="rectangle">
                <size
                    android:width="40dp"
                    android:height="40dp" />
                <solid android:color="#F86F05" />
            </shape>
        </item>
    
        <item android:top="10dp">
            <shape android:shape="rectangle">
                <size
                    android:width="30dp"
                    android:height="30dp" />
                <solid android:color="#B31F19" />
            </shape>
        </item>
    
    </layer-list>
    

    和截图一样。

    如果您想要渐变而不是纯色,请将纯色更改为渐变并设置 startColor、endColor 和角度。

    【讨论】:

      【解决方案3】:

      将这些代码放在你的activity_main中:

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">
      
          <LinearLayout
              android:gravity="center"
              android:background="#F66D08"
              android:layout_width="match_parent"
              android:layout_height="20dp">
      
          </LinearLayout>
      
          <LinearLayout
              android:background="@drawable/introslidergradiant"
              android:layout_width="match_parent"
              android:layout_height="100dp"></LinearLayout>
      </LinearLayout>
      

      并使用 introslidergradiant.xml 名称创建 drawble res 文件:

      <?xml version="1.0" encoding="utf-8"?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android">
          <item>
              <shape>
                  <gradient
                      android:angle="90"
                      android:startColor="#A31720"
                      android:endColor="#E1350B"
                      android:type="linear" />
              </shape>
          </item>
      </selector>
      

      【讨论】:

        【解决方案4】:

        你必须在drawable中制作一个xml文件

        <shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle" >
        <gradient
        android:type="linear"
        android:startColor="#FFFF820D"
        android:endColor="#FFA32815"
        android:angle="90"/>
        </shape>
        

        使用以下代码,或者您可以生成自己的渐变here,只需设置颜色并选择android选项,它就会为您生成渐变。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-10-01
          • 1970-01-01
          • 2021-10-03
          • 2017-02-07
          • 2021-02-14
          • 1970-01-01
          • 2014-12-18
          相关资源
          最近更新 更多