Android - 更改边距的背景颜色
Android - Change background color of margin
我有一个名为 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
并转到另一个片段,然后 returns 到 HostFragment
,边距将更新其颜色。我不知道如何或为什么 - 我在 HostFragment
中没有代码来更新颜色。 HostFragment
中的代码仅处理嵌套片段的换入和换出。
问题: 我需要立即更新边距颜色,所以在 onResume()
中,我尝试了类似 mTableLayout.setBackgroundColor(...)
甚至 mView.setBackgroundColor(...)
(mView
是我在 onCreateView()
中膨胀的布局)。这仍然不起作用,并且颜色只会在用户离开并返回时更新。
问题: 一旦用户 returns HostFragment
来自另一个 Activity
( 即 在用户 returns 从设置之后)?
提前致谢!
无法设置边距的颜色。不过,要实现类似的效果,需要做两件事。
1) 使用填充而不是边距。
边距在元素外部,而填充在元素内部。这意味着该元素的大小会变大,并且您为元素指定的背景色颜色也将应用于内容周围的区域。
2) 使用边框或可绘制对象。
这种方法需要做更多的工作,但可配置性非常高。创建边框只需将可绘制对象设置为背景,并为其指定描边宽度和颜色即可。有关详细信息(和示例实现),请参阅 .
有关边距、填充、边框等的更多信息,请参阅 http://www.w3schools.com/css/css_boxmodel.asp。该网站为 CSS 解释了它,但这个概念几乎在任何地方都是相同的。
为了改变颜色 .setBackgroundColor(...)
应该在 onResume()
中工作,但你应该知道,正如上面所指出的,边距区域是 space在其父视图的引用内离开您的视图外部。这就是为什么更改视图的背景颜色不会影响边距的原因。你可以做的是添加一个 FrameLayout
来包装你的 TableLayout
,这样你的 TableLayout
就有一个参考来设置边距。在这种情况下,您应该能够更改 FrameLayout
的背景,它应该会影响所需的边距区域。
在下图中,红色矩形代表您的 TableLayout
,如您在左侧所见,它是 HostFragment
的根视图,边距区域在 reach.On 右边,你的 HostFragment
的根视图是 FrameLayout
,红色矩形仍然是你的 TableLayout
。在后一种情况下,您可以更改 FrameLayout
.
的颜色
尝试使用 paddingTop
而不是 marginTop
,然后将 onResume
中的视图颜色更改为 mView.setBackgroundColor(...)
。
- 边距 是视图外部的 space,因此视图的背景颜色不会反映在边距 space 中。
- Padding 是视图内部的 space,视图的背景颜色也将应用于填充 space。
试试这个,
<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>
最好的方法是使用背景资源指定具有不同颜色的多个形状并使用边距或填充..
我有一个名为 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
并转到另一个片段,然后 returns 到 HostFragment
,边距将更新其颜色。我不知道如何或为什么 - 我在 HostFragment
中没有代码来更新颜色。 HostFragment
中的代码仅处理嵌套片段的换入和换出。
问题: 我需要立即更新边距颜色,所以在 onResume()
中,我尝试了类似 mTableLayout.setBackgroundColor(...)
甚至 mView.setBackgroundColor(...)
(mView
是我在 onCreateView()
中膨胀的布局)。这仍然不起作用,并且颜色只会在用户离开并返回时更新。
问题: 一旦用户 returns HostFragment
来自另一个 Activity
( 即 在用户 returns 从设置之后)?
提前致谢!
无法设置边距的颜色。不过,要实现类似的效果,需要做两件事。
1) 使用填充而不是边距。 边距在元素外部,而填充在元素内部。这意味着该元素的大小会变大,并且您为元素指定的背景色颜色也将应用于内容周围的区域。
2) 使用边框或可绘制对象。 这种方法需要做更多的工作,但可配置性非常高。创建边框只需将可绘制对象设置为背景,并为其指定描边宽度和颜色即可。有关详细信息(和示例实现),请参阅 .
有关边距、填充、边框等的更多信息,请参阅 http://www.w3schools.com/css/css_boxmodel.asp。该网站为 CSS 解释了它,但这个概念几乎在任何地方都是相同的。
为了改变颜色 .setBackgroundColor(...)
应该在 onResume()
中工作,但你应该知道,正如上面所指出的,边距区域是 space在其父视图的引用内离开您的视图外部。这就是为什么更改视图的背景颜色不会影响边距的原因。你可以做的是添加一个 FrameLayout
来包装你的 TableLayout
,这样你的 TableLayout
就有一个参考来设置边距。在这种情况下,您应该能够更改 FrameLayout
的背景,它应该会影响所需的边距区域。
在下图中,红色矩形代表您的 TableLayout
,如您在左侧所见,它是 HostFragment
的根视图,边距区域在 reach.On 右边,你的 HostFragment
的根视图是 FrameLayout
,红色矩形仍然是你的 TableLayout
。在后一种情况下,您可以更改 FrameLayout
.
尝试使用 paddingTop
而不是 marginTop
,然后将 onResume
中的视图颜色更改为 mView.setBackgroundColor(...)
。
- 边距 是视图外部的 space,因此视图的背景颜色不会反映在边距 space 中。
- Padding 是视图内部的 space,视图的背景颜色也将应用于填充 space。
试试这个,
<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>
最好的方法是使用背景资源指定具有不同颜色的多个形状并使用边距或填充..