安卓碎片屏幕重叠。

5
我有一个使用片段的应用程序。 在我的一个用户的设备(HTC One)上,片段互相重叠,导致他的屏幕看起来很混乱: enter image description here 我试图在自己的硬件上复制它,但不是HTC One。 我还尝试使用他所拥有的Android版本4.1.2,一切正常。 除了跑去买HTC One之外,有人有任何建议吗?
当我添加新片段时,我这样做
 Fragment f = new MyFragment();
 FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
 ft.replace(R.id.mainContent, f);
 ft.addToBackStack(null);
 ft.commit();

我的XML布局(仅保留相关部分):

<RelativeLayout>
    <LinearLayout>
        <!-- My home screen content is here -->
    </LinearLayout>

    <!-- This is where the fragment gets placed -->
    <LinearLayout android:id="@+id/mainContent"
                  android:orientation="vertical"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent">
    </LinearLayout>
</RelativeLayout>

更新

添加的片段具有以下布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/app_bg"    
    tools:context=".DeviceListActivity" >

    <!-- snipped! (for brevity) -->
</RelativeLayout>

我一直在尝试 - 我发现如果我删除android:background,我可以复制这个问题,这让我相信HTC One由于某种原因导致片段的背景属性被忽略。


HTC One使用xxhdpi资源,您提供吗?看起来可能有关。 - Dalmas
@Dalmas,这不会导致布局问题,而是会导致资源问题,即加载错误的资源。 - Christopher Perry
4个回答

4
我遇到过类似的情况,以下方法对我有用。检查savedInstanceState是否为非空,在这种情况下不要进行片段事务。这将解决此问题。
if (homeFragment == null) {
    if (savedStateInstance != null) {
    homeFragment = (HomeFragment) fragmentManager.findFragmentByTag(HOME_FRAGMENT);
    return;
}
// tie new fragment
homeFragment = new HomeFragment();
homeFragment.setArguments(bundle);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(homeFragment, HOME_FRAGMENT);
transaction.commit();
}

希望这有所帮助。

1
结果表明问题是资源问题,但不清楚原因...
我只有ldpi文件夹中的背景信息;通常更高密度的设备会使用任何可用的图形,然而在这种情况下,设备却忽略了该文件。是一个bug吗?不知道 - 无论如何,解决方法是创建一个与相同名称匹配的xxdpi文件夹的资源。

0

我已经完成了...

我的主活动布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    android:padding="10dp">

    <RelativeLayout
        android:id="@+id/registerContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/usernameEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="15dp"
            android:layout_marginTop="15dp"
            android:hint="USERNAME"
            android:textColorHint="#FFFFFF"
            android:textColor="#FFFFFF"
            android:background="@drawable/edittext_style"
            android:textAlignment="center"/>

        <EditText
            android:id="@+id/passEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="15dp"
            android:layout_marginTop="15dp"
            android:inputType="textPassword"
            android:hint="PASSWORD"
            android:textColorHint="#FFFFFF"
            android:textColor="#FFFFFF"
            android:textAlignment="center"
            android:background="@drawable/edittext_style"
            android:layout_below="@id/usernameEditText"/>


        <EditText
            android:id="@+id/phoneEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="15dp"
            android:layout_marginTop="15dp"
            android:inputType="phone"
            android:hint="PHONE NUMBER"
            android:textColorHint="#FFFFFF"
            android:textColor="#FFFFFF"
            android:background="@drawable/edittext_style"
            android:textAlignment="center"
            android:layout_below="@id/passEditText"/>

        <EditText
            android:id="@+id/emailEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="15dp"
            android:layout_marginTop="15dp"
            android:inputType="textEmailAddress"
            android:hint="EMAIL"
            android:textColorHint="#FFFFFF"
            android:textColor="#FFFFFF"
            android:background="@drawable/edittext_style"
            android:textAlignment="center"
            android:layout_below="@id/phoneEditText"/>

        <Button
            android:id="@+id/registerEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/emailEditText"
            android:text="REGISTER"
            android:layout_marginTop="20dp"
            android:background="#0b999b"
            android:textColor="#000000"
            android:textSize="18dp"/>


    </RelativeLayout>

    <FrameLayout
        android:id="@+id/otpfragmentcontainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></FrameLayout>


</RelativeLayout>

重叠布局的片段是 otp_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="@color/colorPrimary">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/textviewOTP"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Enter Your OTP"
            android:textColor="#0b999b"
            android:textStyle="bold"
            android:textSize="24dp"
            android:textAlignment="center"
            android:layout_marginTop="200dp"
            android:layout_marginBottom="5dp"/>

        <EditText
            android:id="@+id/otpEditText"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:padding="15dp"
            android:layout_marginTop="15dp"
            android:inputType="phone"
            android:hint="EX: 456789"
            android:textColorHint="#FFFFFF"
            android:textColor="#FFFFFF"
            android:background="@drawable/edittext_style"
            android:textAlignment="center"
            android:layout_below="@id/textviewOTP"/>

        <Button
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="ENTER"
            android:background="#0b999b"
            android:layout_centerHorizontal="true"
            android:layout_below="@id/otpEditText"/>


    </RelativeLayout>

</LinearLayout>

我所做的是

        registerContainer.setVisibility(View.GONE);

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        OTPFragment otpFragment = new OTPFragment();
            fragmentTransaction.add(R.id.otpfragmentcontainer, otpFragment);
            fragmentTransaction.commit();

which disable registerContainer and display my fragment layout

0
你可以尝试将片段容器更改为FrameLayout,并查看是否仍然发生此问题。由于您在Fragment上设置了背景,它应该覆盖先前的背景。不确定为什么这只会在那个设备上发生。
<!-- This is where the fragment gets placed -->
<FrameLayout android:id="@+id/mainContent"
             android:layout_width="match_parent"
             android:layout_height="match_parent">

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接