relativelayout背景图片+颜色
我有一个 RelativeLayout
,我需要有一个黑色背景和一个位于其中间的小图像。我使用了这段代码:
<RelativeLayout
android:id="@+id/bottomBox"
android:layout_weight="4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bottom_box_back"
android:layout_marginTop="4dp"
/>
其中 @drawable/bottom_box_back
是:
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/logo_icon"
android:tileMode="disabled"
android:gravity="center"
android:filter="true"
/>
这样我就可以将图像居中。但背景仍然与主窗口一样是灰色的。我需要一个黑色的背景来覆盖整个 RelativeLayout
空间的图像。有什么建议吗?
I have a RelativeLayout
where I need to have a black background and a small image exactly in the middle of it. I used this code:
<RelativeLayout
android:id="@+id/bottomBox"
android:layout_weight="4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bottom_box_back"
android:layout_marginTop="4dp"
/>
where the @drawable/bottom_box_back
is:
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/logo_icon"
android:tileMode="disabled"
android:gravity="center"
android:filter="true"
/>
In this way I have the image centered. But the background is still gray as the main window. I need a black back to the image spread all over the RelativeLayout
space. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会将您的RelativeLayout的背景设为黑色,然后将图像放入ImageView并将其居中于RelativeLayout的中间,所以:
希望这对您有帮助
I would make the background of your RelativeLayout black and then put the Image into an ImageView and center that in the middle of the RelativeLayout, so:
Hope this helps you
另一种选择是使用 9 块图像。实际上,您可以在 9 块图像上有多个可拉伸区域,因此您所需要做的就是将所需的图像放在中间区域的中心,然后将其周围用您想要的黑色作为背景色。然后,在声明可拉伸区域时,选择图像上方、下方、左侧和右侧的区域作为可拉伸区域。当应用程序拉伸区域时,它会均匀地拉伸它们,因此只要可拉伸区域的大小相同,图像就会保持在中心。
我知道这已经有了一个公认的答案,但我认为这是完成同一件事的另一种巧妙方法,而无需向布局添加另一个图像视图。
Another option is to use a 9-patch image. You can actually have multiple stretchable areas on a 9-patch image so all you have to do is put the image you want centered in the middle area, and then surround it in the black you want as the background color. Then when declaring the stretchable areas, select the areas above, below, and to the left and right of your image as stretchable. When the app stretches the areas it stretches them evenly so the image will stay in the center as long as the stretchable areas are the same size.
I know this already has an accepted answer but I thought this was another neat way of accomplishing the same thing without having to add another image view to the layout.