我无法将渐变背景应用于线性布局.

从我所读到的来看,这应该是相对简单的,但它似乎就是行不通.为了参考起见,我在2.1上开发-更新1.

Header_bg.xml:

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

main_header.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="50dip"
    android:orientation="horizontal"
    android:background="@drawable/header_bg">
</LinearLayout>

如果我把@drawable/header_bg改成一种 colored颜色 ,例如#FF0000,效果非常好.我是不是漏掉了什么明显的东西?

推荐答案

好的,我用 Select 器解决了这个问题.见下面的代码:

主_头.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="50dip"
    android:orientation="horizontal"
    android:background="@drawable/main_header_selector">
</LinearLayout>

主标题 Select 器.xml:

<?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="#FFFF0000"
            android:endColor="#FF00FF00"
            android:type="linear" />
    </shape>
</item>
</selector>

希望这能帮助有同样问题的人.

Android相关问答推荐

如何将结果从viewModelScope传递到活动

NativeScript在`ns run android`上重复Kotlin类

无法使用MenuItemColors.Copy()

如何更新Kotlin中的显示?

不能在LazyGrid-Jetpack Compose中使用填充最大宽度或填充父项最大宽度

在androidStudio中,如何使用带有ResolutionStrategy的ResolutionSelector而不是setTargetResolve()?

如何修复Google Play市场中有关已删除广告库的错误消息?

从不可组合回调打开可组合屏幕

使用 JNI 从 Android 应用程序中使用 Kotlin/Native 预构建共享库

Camera2 将图像从 ImageReader 传递到 MediaRecorder

为什么 Android Studio 中的 theme.xml 目录没有任何原色

有没有办法让协程通道在接收时遵循特定的顺序而不是先进先出

页面标题未显示在内容页面上

具有数据库和升级潜力的移动应用程序开发(Android)供朋友使用

在单元测试下断言协程未完成

我的观点在jetpack compose中相互重叠

为 AlertDialog 的消息文本设置自定义字体

如何在stroke android drawable中设置渐变

如何删除 Ktor 客户端 2.0.0 的默认标头

dagger2 抛出错误:如果没有 @Provides-annotated 方法就无法提供.在我的 android 项目的构建中