如何将背景渐变添加到fragment.xml类

时间:2014-01-08 14:01:04

标签: android xml android-fragments master-detail

添加背景渐变是我所知道的一个非常基本的事情,但我被抛出“根元素之后的文档中的标记必须形成良好”

我确信我正在为你们中的许多人做一些非常明显的事情,但如果我能在这里看到我的方式错误,我将不胜感激。

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/articlelist"
android:name="com.MSmith.test.ArticleListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
tools:context=".ArticleListActivity"
tools:layout="@android:layout/list_content"/>


<style android:background="#A9A9A9"/>

尝试将android:background =“”添加到但是尽管没有显示任何错误,但模拟器/图形显示没有变化。

1 个答案:

答案 0 :(得分:1)

像这样定义一个drawable:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <gradient
        android:angle="315"
        android:endColor="@color/mainMenuGradient_endColor"
        android:startColor="@color/mainMenuGradient_startColor" />

</shape>

并将此drawable设置为片段根布局中的背景,并且您将具有渐变。

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/articlelist"
android:name="com.MSmith.test.ArticleListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
tools:context=".ArticleListActivity"
tools:layout="@android:layout/list_content"
android:background="@drawable/yourdrawablehere"/> <----- this might work

或直接在Fragment的中添加