簡體   English   中英

如何在ScrollView中設置背景顏色?

[英]How can I set the background color in a ScrollView?

我有以下簡單的布局

    <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <RelativeLayout
        android:id="@+id/answerMainFrame"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/background"
        android:isScrollContainer="true"
        android:onClick="toAnswer" >

        <ImageView
            android:id="@+id/answer_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:contentDescription="@string/question_img_cd" />

        <TextView
            android:id="@+id/answer"
            style="@style/Question"
            android:layout_below="@id/answer_img" />
    </RelativeLayout>

</ScrollView>

但有時,根據ImageView和TextView的大小,它不會填滿屏幕的高度。 沒關系。 我只希望屏幕的其余部分為白色而不是黑色。

我已經嘗試將android:background="@color/background"設置為ScrollView的白色,但是我得到了相同的結果。

我也嘗試將android:layout_height="wrap_content"設置為Relativelayout,但它顯示警告。

我該怎么辦?

謝謝。

ScrollView高度更改為match_parent並將白色設置為背景。 所以這樣根據您的代碼:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="fill_parent"
                  android:layout_height="match_parent"
                  android:background="@color/background">
...

注意。 關於RelativeLayout的警告很容易解釋。 如果將其高度設置為wrap_content則無法執行此操作,因為其內部包含的元素要求其父級具有一組固定的尺寸,以便能夠執行諸如附着到底部或中心或其他任何操作。

起初,我對此也有些困惑。

那樣做

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff >

<RelativeLayout
    android:id="@+id/answerMainFrame"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/background"
    android:isScrollContainer="true"
    android:onClick="toAnswer" >

    <ImageView
        android:id="@+id/answer_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:contentDescription="@string/question_img_cd" />

    <TextView
        android:id="@+id/answer"
        style="@style/Question"
        android:layout_below="@id/answer_img" />
</RelativeLayout> </ScrollView>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM