Android - 改变边距的背景颜色

7

我有一个名为HostFragment的片段,它嵌套了一到四个其他片段。

这是HostFragment的布局:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/hostFragmentLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="12dp">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <RelativeLayout
            android:id="@+id/fragmentContainer1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"/>

        <RelativeLayout
            android:id="@+id/fragmentContainer2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"/>

    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <RelativeLayout
            android:id="@+id/fragmentContainer3"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"/>

        <RelativeLayout
            android:id="@+id/fragmentContainer4"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"/>

    </TableRow>

</TableLayout>

重要的部分是 android:layout_marginTop="12dp"
背景:嵌套片段覆盖了整个 HostFragment,除了这个边距。当嵌套片段改变它们的背景颜色(通过调用 Canvas#drawColor)时,HostFragment 需要同样改变这个边距的颜色以匹配。我将所需的颜色存储在 SharedPreferences 中。
行为:如果用户从 HostFragment 转到 SettingsActivity,更改颜色,然后返回到 HostFragment,嵌套片段将立即更改它们的颜色(通过它们的 onResume() 方法),但是 HostFragment 的边距仍然是旧颜色。如果用户然后离开 HostFragment 并转到另一个片段,然后返回到 HostFragment,边距将更新其颜色。我不知道为什么 - 我没有在 HostFragment 中编写代码来更新颜色。HostFragment 中的代码只涉及交换嵌套片段。 问题: 我需要立即更新边距颜色,所以在onResume()中,我尝试了一些类似于mTableLayout.setBackgroundColor(...)甚至是mView.setBackgroundColor(...)的方法(mView是我在onCreateView()中填充的布局),但这仍然无效,只有当用户离开并返回时,颜色才会更新。

问题: 当用户从其他Activity(例如:从设置页面)返回到HostFragment后,如何根据SharedPreferences中的一个int值来改变边距颜色呢?

提前致谢!


尝试使用 paddingTop 而不是 marginTop,然后在 onResume 中通过 mView.setBackgroundColor(...) 更改视图的颜色。 - Abhishek V
@AbhishekV 将您的评论添加为答案。 - pez
@pez 完成。已将其添加为答案。 - Abhishek V
5个回答

14
尝试将marginTop更改为paddingTop,然后在onResume中通过mView.setBackgroundColor(...)改变视图的颜色。
  • Margin是视图外部的空间,所以视图的背景颜色不会反映在边距空间中。
  • Padding是视图内部的空间,并且赋予视图的背景颜色也会应用于填充空间。

2

try this,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/c1_cnxlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/black" >

    <RelativeLayout 
        android:id="@+id/c2_cnxlayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:background="@android:color/darker_gray" />

</RelativeLayout>

1
设置边距的颜色是不可能的。但要实现类似的效果,有两件事情需要做。
1)使用内边距代替外边距。 外边距在元素外部,而内边距在元素内部。这意味着元素将增大,并且您为其设置的背景颜色也将应用于内容周围的区域。
2)使用边框或可绘制对象。 这种方法需要更多的工作,但是非常易于配置。创建边框只需将可绘制对象设置为背景,并给它一个描边宽度和颜色即可。有关更多信息(以及示例实现),请参见https://dev59.com/dGsy5IYBdhLWcg3w9Svk#8203840
有关边距、内边距、边框等的更多信息,请参见http://www.w3schools.com/css/css_boxmodel.asp。该网站解释了CSS中的概念,但在任何地方基本上都是相同的。

0
为了改变颜色,.setBackgroundColor(...) 应该在 onResume() 中工作,但您应该注意,正如上面指出的那样,边缘区域是在其父视图的参考系之外留下的空间。这就是为什么更改视图的背景颜色不会对边距产生影响的原因。您可以添加一个包装您的TableLayoutFrameLayout,以便您的TableLayout具有设置边距的参考。在这种情况下,您应该能够更改FrameLayout的背景颜色,并且它应该影响所需的边距区域。
在下面的图像中,红色矩形表示您的TableLayout,如您在左侧看到的,它是您HostFragment的根视图,而边距区域在您的视野之外。在右侧,您的HostFragment的根视图是一个FrameLayout,红色矩形仍然是您的TableLayout。在后一种情况下,您可以更改FrameLayout的颜色。

图片:http://oi59.tinypic.com/jz8q46.jpg


0

最好的方法是使用背景资源指定多个具有不同颜色的形状,并使用边距或填充。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接