滚动查看滚动视图的子项(线性布局)的背景颜色

时间:2013-11-06 12:10:41

标签: android android-layout android-scrollview

我正在使用scrollview并得到一个我无法弄清楚的奇怪问题。

我附加了图形视图的屏幕截图,因为我为布局背景设置了一种颜色,但在滚动视图时它会被更改。

这是我的代码。

OnCreate of Activity。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_discussionlist);


        ll_comments = (LinearLayout) findViewById(R.id.ll_comments);

        for (int i = 0; i < 10; i++) {
            View view = LayoutInflater.from(DiscussionListActivity.this)
                    .inflate(R.layout.comments, null);

            ll_comments.addView(view, ll_comments.getChildCount());
            Log.print(" ll_comments.getChildCount() ="
                    + ll_comments.getChildCount());

        }
    }

这里是activity_discussionlist.xml

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

    <RelativeLayout
        android:id="@+id/ll_discussion_content"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/txt_submenu_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="-5dp"
            android:background="@drawable/submenu_bg"
            android:gravity="center"
            android:paddingBottom="10dp"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:paddingTop="10dp"
            android:text="SubMenu"
            android:textColor="#FFFFFF" />

        <ImageView
            android:id="@+id/img_reply"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="10dp"
            android:src="@drawable/ic_reply"
            android:visibility="visible" />
    </RelativeLayout>

    <ScrollView
        android:id="@+id/sv_discussion"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:fillViewport="true"
        android:scrollbars="none" >

        <LinearLayout
            android:id="@+id/ll_scroll_main"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <RelativeLayout
                android:id="@+id/rl_discussion"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/discussion_bg"
                android:padding="5dp" >

                <HorizontalScrollView
                    android:id="@+id/hl_title"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_marginTop="5dp"
                    android:scrollbars="none" >

                    <TextView
                        android:id="@+id/txt_title"
                        style="@style/TextTitleBold"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:singleLine="true"
                        android:text="Homework Questions" />
                </HorizontalScrollView>

                <TextView
                    android:id="@+id/txt_uploadedby_date"
                    style="@style/TextDate"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/hl_title"
                    android:layout_marginTop="5dp"
                    android:ellipsize="end"
                    android:singleLine="true"
                    android:text="by Ralph on Oct 01,2013 6:15 AM"
                    android:textColor="#BBBBBB" />

                <TextView
                    android:id="@+id/txt_posts"
                    style="@style/TextDate"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/txt_uploadedby_date"
                    android:layout_marginTop="5dp"
                    android:ellipsize="end"
                    android:singleLine="true"
                    android:text="12 Comments" />

                <TextView
                    android:id="@+id/txt_descr"
                    style="@style/TextDescription"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/txt_posts"
                    android:layout_marginTop="5dp"
                    android:ellipsize="end"
                    android:singleLine="false"
                    android:text=" 0 down vote favorite I am setting up images for different devices as per official google docs.As per google docs we should always use dp(density independent pixels) because pixels may varies for different devices. So i have managed images as per dp(density independent pixels). I have put images in drawable xhdpi,hdpi,mdpi and ldpi. it works well for most of devices but for different devices ppi pixels may varies from device to device so dp(density independent pixels) is not fixed so my all calculations according to dp(density independent pixels) goes wrong and cannot be set properly." />
            </RelativeLayout>

            <View
                android:id="@+id/view_separator"
                android:layout_width="fill_parent"
                android:layout_height="10dp"
                android:layout_marginTop="10dp"
                android:background="#BBBBBB" />

            <LinearLayout
                android:id="@+id/ll_comments"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:orientation="vertical"
                android:padding="10dp" >
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

</LinearLayout>

这里是comments.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="20dp" >

    <FrameLayout
        android:id="@+id/comment_identity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/comment_options"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="35dp"
            android:background="#ececec"
            android:gravity="bottom"
            android:orientation="horizontal"
            android:weightSum="2" >

            <LinearLayout
                android:id="@+id/linear_editoption"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="right|center_vertical"
                android:padding="10dp" >

                <ImageView
                    android:id="@+id/img_edit"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/img_edit" />

                <TextView
                    android:id="@+id/text_editoption"
                    style="@style/TextDescriptionBold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:text="Edit" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/linear_deleteoption"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center_horizontal|center_vertical"
                android:padding="10dp" >

                <ImageView
                    android:id="@+id/img_delete"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/img_delete" />

                <TextView
                    android:id="@+id/text_deleteoption"
                    style="@style/TextDescriptionBold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:text="Delete" />
            </LinearLayout>
        </LinearLayout>

        <RelativeLayout
            android:id="@+id/rel_commentphoto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:padding="10dp" >

            <ImageView
                android:id="@+id/img_commentpic"
                android:layout_width="50dp"
                android:layout_height="60dp"
                android:background="@drawable/img_bg"
                android:src="@drawable/profile" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/rel_commentidentity"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/txt_name"
                style="@style/TextDescriptionBold"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="70dp"
                android:layout_marginTop="10dp"
                android:singleLine="true"
                android:text="Ralph Johnson" />


            <TextView
                android:id="@+id/txt_date"
                style="@style/TextDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:layout_toRightOf="@+id/txt_name"
                android:singleLine="true"
                android:text="02 Oct,2013 6:00 AM"
                android:textColor="#BBBBBB" />
        </RelativeLayout>
    </FrameLayout>

    <TextView
        android:id="@+id/txt_comment"
        style="@style/TextDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/comment_identity"
        android:layout_marginTop="5dp"
        android:singleLine="false"
        android:text="This is test comment.Post your comments here." />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_below="@+id/txt_comment"
        android:layout_marginTop="20dp"
        android:background="#DDDDDD" />

</RelativeLayout>

正如大家可以看到我在comments.xml文件中仅将背景颜色设置为线性布局(线性布局的ID为'comment_options')。

我在下面的图片中标记了红色圆圈。请看差异。

enter image description here

1 个答案:

答案 0 :(得分:2)

问题不在于您的布局定义或颜色分配。由于您未在activity_discussionlist.xml中设置基本容器的背景属性,因此为您的app-theme定义的windowBackground属性正在发挥作用。

您目前看到的是一个定义为:

的渐变

screen_background_selector_light.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
    android:startColor="#ffe8e8e8"
    android:endColor="#ffffffff"
    android:angle="270" />
</shape>

因此,如果您的活动的布局是一个没有设置背景属性的空LinearLayout(例如),它将如下所示:

enter image description here

以下是背景可能的来源:

(taken from platforms/android-17/data/res/values/themes.xml)
<style name="Theme.Light">
    <item name="windowBackground">@android:drawable/screen_background_selector_light</item>
    ....

我认为发生了什么:

lightest background      ==>>     #ECECEC on mostly #E8E8E8
lighter background       ==>>     #ECECEC on (#E8E8E8 + a bit of #FFFFFF)
less lighter background  ==>>     #ECECEC on (#FFFFFF + a bit of #E8E8E8)
dark background          ==>>     #ECECEC on mostly #FFFFFF

行动中:

enter image description here

在上图中,所有四个LinearLayouts(每个在TextView上方)都是相同的,其背景设置为“#ECECEC”。视觉区别来自活动的背景,这是一个渐变。

因此,如果您将父容器的背景属性(activity_discussionlist.xml中的根级LinearLayout)设置为#ECECEC或您喜欢的任何其他颜色,则此问题将得到解决。渐变是你不想要的。