开发者

Android: RelativeLayout background drawable disappears

开发者 https://www.devze.com 2023-04-12 19:51 出处:网络
I have an xml layout for a Fragment (android.support.v4.app.Fragment). When this Fragment is added for the first time, the background drawable displays fine. But when this Fragment is replaced with an

I have an xml layout for a Fragment (android.support.v4.app.Fragment). When this Fragment is added for the first time, the background drawable displays fine. But when this Fragment is replaced with another Fragment, then later replaced back in again (by creating a new instance and using FragmentTransaction.replace()), the background drawable disappears (but not in all cases, see below). Here is the layout xml for the Fragment:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <include layout="@layout/footer_photos" />
    <RelativeLayout
        android:id="@+id/pageLayout"
        android:background="@drawable/body_background2"
      开发者_运维问答  android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/footer">
        <ImageView
            android:id="@+id/page"
            android:layout_height="500dp"
            android:layout_width="400dp"
            android:layout_centerInParent="true"
            android:background="#FFF" />
    </RelativeLayout>
</RelativeLayout>

The background drawable in question is in the RelativeLayout with the id "pageLayout".

Possible clues:

  • When I remove the ImageView in there, the background drawable shows up fine.
  • The other Fragments have similar layout xmls (with the same background drawable), but without ImageViews inside of them, and they work fine.
  • One of these Fragments has a WebView and buttons inside of it, and this one's background shows up fine.
  • After replacing the "WebView Fragment" with the Fragment in question, the background shows up fine (!?).

EDIT: The same issue consistently occurs if an image is loaded into the ImageView then a dialog-themed activity is launched on top of it, then finished. My solution below fixes both cases.


Found a better solution than my last one. In onResume() for the Fragment, just need to do:

    getView().post(new Runnable() {
        @Override
        public void run() {
            View v = getView();
            if (v != null)
                v.invalidate();
        }
    });

It causes a flicker, but at least it seems to work consistently.


i had a similar problem. With a relativelayout, there was an imageView and a textView. I resolved with a call to bringToFront method:

myImageView.bringToFront();

Hope this helped.


Okay, I fixed this issue while trying to fix another issue. The issue was solved by calling setLayoutParams() on the page ImageView (with whatever layout params). This caused the background of the pageLayout RelativeLayout to show up again. The cause of the issue remains unknown...


The fragments are replaced , along with their background, which means that

  1. when a fragment A is replaced by fragment B, the background changes to the background of fragment B.

  2. If you don't give any background to fragment B, then you can see fragment B over fragment A.

0

精彩评论

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

关注公众号