片段重叠

时间:2017-01-24 11:51:06

标签: android android-fragments

我在测试我的应用程序时创建了一个应用程序,或者我单击了后退按钮,我的片段与其他片段重叠。我不知道它为什么会发生。请帮帮我,我需要帮助.. 我的代码

<div class="wrapper">
  <div class="row">
    <article class="col-6">
      <h1>Article 1</h1>
      <p>Shown in left column</p>
    </article>
    <article class="col-6">
      <h1>Article 2</h1>
      <p>Shown in right column</p>
    </article>
  </div>
  <div class="row">
    <article class="col-6">
      <h1>Article 3</h1>
      <p>Shown in left column</p>
    </article>
    <article class="col-6">
      <h1>Article 4</h1>
      <p>Shown in left column</p>
    </article>
  </div>
  <div class="row">
    <article class="col-6">
      <h1>Article 5</h1>
      <p>Shown in right column</p>
    </article>
  </div>
</div>

activity_main.xml中

public class MainActivity extends AppCompatActivity {

    private SharedPreferences pref;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pref = getPreferences(0);
        initFragment();
    }

    private void initFragment(){
        Fragment fragment;
        if(pref.getBoolean(Constants.IS_LOGGED_IN,false)){
            fragment = new ProfileFragment();
        }else {
            fragment = new LoginFragment();
        }
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.fragment_frame,fragment);
        ft.commit();
    }
}

Uploads.java

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
    android:background="#ff51b46d"
    tools:context="com.example.hldev.docup.MainActivity">

    <LinearLayout 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"
        tools:context="com.example.hldev.docup.MainActivity">

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

    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

3 个答案:

答案 0 :(得分:1)

只需在XML文件中为新<fragment />设置背景颜色,否则它将是透明的并显示已替换的片段。

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="schemas.android.com/apk/res/android"; 
    xmlns:tools="schemas.android.com/tools"; 
    android:id="@+id/frag24" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
  <RelativeLayout 
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@color/green"> 
      <!-- Your Content -->  
      </RelativeLayout> 
</FrameLayout >

答案 1 :(得分:0)

设置activity_uploads父级布局的白色背景色。也适用于Fragment中使用的所有布局。

答案 2 :(得分:0)

用下面的代码替换你的代码。

<RelativeLayout xmlns:android="schemas.android.com/apk/res/android"
    xmlns:tools="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="@android:color/holo_green_dark"
    tools:context="com.example.hldev.docup.Uploads">