简体   繁体   中英

Android : One background for two fragments

I try to make an app but I have an issue with the background.

架构图

The above image shows what should look like the app. Every rectangle of color is a fragment and the red one already have the correct part of the background. But now I try to figure out how can i make the green rectangle background respect this rules :

  • Image width needs to match parent
  • The image needs to keep his ratio
  • Image need to "stick" the bottom (in that way the upper part shows in the red rectangle won't appears in the green one)

In the green fragment I have a ConstraintLayout and an ImageView like above :

<ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/ss_background_blur"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"/>

The @drawable/ss_background_blur is my PNG image.

Thx

Set the background of the rootlayout to green and add android:layout_margin="2dp" to the imageview .

Hope this helps.

As Neo Shen pointed out in the comments, the only thing you need to do is set the background property of the container element to the drawable resource (the image).

If you have an Activity which only contains three Fragment elements, then you can set the background of the main Activity layout.

You can see and clone a quick demo I put together here:

https://github.com/sipox11/full-background-android-app

This is the result:

演示应用

Note: I did it very quickly so obviously the tabs are not real tabs and the top bar is not a real nav bar, but you get the idea.

In this case the background image is actually below the tabs area, but if you want it to start on top of it, you can wrap the top and middle fragments within a LinearLayout and set the background image to that container.

Hope it helps, Cheers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM