开发者

RelativeLayout background image + color

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 Relative开发者_如何学GoLayout space. Any suggestions?


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:

<RelativeLayout
    android:background="@color/yourBlackColor">

    <ImageView
        android:layout_centerInParent="true">
    </ImageView>

</RelativeLayout>

Hope this helps you


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜