2016-12-14 127 views
0

我正在我的Android項目的最後幾天工作,但我只是有一個問題。我讀過很多教程,我讀的每個解決方案都是失敗的。 我將不勝感激,如果任何人都可以幫助我。 問題:碎片僅位於contentHome.xml的頂部。但我想,佈局應該有機會到達片段。Android:活動與片段(片段頂部)

HomeActivity:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_home); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
    this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    drawer.addDrawerListener(toggle); 
    toggle.syncState(); 

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(this); 
} 

@Override 
public void onBackPressed() { 
    DrawerLayout drawer = (DrawerLayout) indViewById(R.id.drawer_layout); 
    if (drawer.isDrawerOpen(GravityCompat.START)) { 
     drawer.closeDrawer(GravityCompat.START); 
    } else { 
     super.onBackPressed(); 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.home, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.menu_search) { 
     return true; 
    } 

    return id == R.id.location || super.onOptionsItemSelected(item); 

} 

@SuppressWarnings("StatementWithEmptyBody") 
@Override 
public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
    // Handle navigation view item clicks here. 
    int id = item.getItemId(); 

    if (id == R.id.nav_home) { 

    } else if (id == R.id.nav_account) { 
     AccountFragment accountFragment = new AccountFragment(); 
     FragmentManager manager = getSupportFragmentManager(); 
     manager.beginTransaction().replace(R.id.app_bar,accountFragment).commit(); 

    } else if (id == R.id.nav_favorite) { 

    } else if (id == R.id.nav_inbox) { 

    } else if (id == R.id.nav_help) { 

    } else if (id == R.id.nav_settings) { 

    } else if (id == R.id.nav_logout) { 

    } 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    drawer.closeDrawer(GravityCompat.START); 
    return true; 
} 

}

contentHome.xml:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:context="com.test.john.test.HomeActivity"> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/content_home" 
    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" 
    tools:context="com.test.john.test.LoginActivity"> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:srcCompat="@drawable/test_logo" 
     android:id="@+id/imageView2" 
     android:layout_marginBottom="240dp" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentStart="true" 
     android:contentDescription="@string/testLogo"/> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:background="@drawable/btn_login" 
     android:textColor="@color/iconsText" 
     android:text="@string/newMachineText" 
     android:id="@+id/btn_NewMachine" 
     android:gravity="center" 
     android:clickable="true" 
     android:layout_marginBottom="45dp" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentStart="true" /> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:background="@drawable/btn_login" 
     android:textColor="@color/iconsText" 
     android:text="@string/accountOverviewText" 
     android:id="@+id/btn_AccountOverview" 
     android:gravity="center" 
     android:clickable="true" 
     android:layout_marginBottom="20dp" 
     android:layout_above="@+id/btn_NewMachine" 
     android:layout_alignParentStart="true" /> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:background="@drawable/btn_login" 
     android:textColor="@color/iconsText" 
     android:text="@string/searchMachineText" 
     android:id="@+id/btn_SearchMachine" 
     android:gravity="center" 
     android:clickable="true" 
     android:layout_marginBottom="18dp" 
     android:layout_above="@+id/btn_AccountOverview" 
     android:layout_alignParentStart="true" /> 
</RelativeLayout> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:popupTheme="@style/AppTheme.PopupOverlay" /> 

</android.support.design.widget.AppBarLayout> 

AccountFragment:

<FrameLayout 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.test.john.test.AccountFragment"> 

<!-- TODO: Update blank fragment layout --> 


<CalendarView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/calendarView" /> 

+0

我讀過很多計算器問題是這樣的: http://stackoverflow.com/questions/18309815/fragments-displayed-over-each-other 但該解決方案並不適合我。 – Bodydrop

回答