【问题标题】:Create a custom gradient with 2 shapes创建具有 2 个形状的自定义渐变
【发布时间】:2016-05-28 14:34:05
【问题描述】:

我想在 Android 中创建渐变背景。我使用angeroustools.com 来生成这两个形状。不完全确定如何实现它们。

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<gradient 
android:type="linear"
android:centerX="100%" 
android:startColor="#7F000000" 
android:centerColor="#FFffffff" 
android:endColor="#FFffffff" 
android:angle="90"/>
</shape>

-------------------------------------------------
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<gradient 
android:type="radial"
android:centerX="50%" 
android:centerY="50%" 
android:startColor="#7F000000" 
android:centerColor="#FFffffff" 
android:endColor="#FFffffff" 
android:gradientRadius="100"/>
</shape>

【问题讨论】:

    标签: android gradient shape


    【解决方案1】:

    尝试通过在可绘制文件夹中创建 yourFile.xml 来使用图层列表

    <item>
       <layer-list>
        <item> 
               <shape xmlns:android="http://schemas.android.com/apk/res/android"      android:shape="rectangle" >
               <gradient 
                android:type="linear"
                android:startColor="#7F000000" 
                android:centerColor="#FFffffff" 
                android:endColor="#FFffffff" 
                android:angle="90"/>
                </shape>
          </item>
          <item>
                <shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle" >
                <gradient 
                android:type="radial"
                android:centerX="0.5" 
                android:centerY="0.5" 
                android:startColor="#7F000000" 
                android:centerColor="#FFffffff" 
                android:endColor="#FFffffff" 
                android:gradientRadius="100"/>
                </shape>
          </item>
       </layer-list>
    </item>
    

    然后像android:background="@drawable/yourFile.xml"那样使用它

    【讨论】:

      【解决方案2】:

      首先,您需要为 shape1.xmlshape2.xml 等形状创建 2 个 xml 文件 并将drawable文件夹放入res文件夹中。

      然后通过调用

      根据需要使用它们

      android:background="@drawable/shape1" 或者 android:background="@drawable/shape2"

      更多详情See here

      【讨论】:

      • 如果我没记错的话,你只能设置一个android:background参数。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多