I am working on the last days of my Android project but I just have a problem. I have read many tutorials and every solution I read was a fail. I would appreciate, if anyone can help me. Problem: The Fragments are just on the top of the contentHome.xml. But I want, that the layout should chance to the fragment.
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" />
Maybe using a layout as a container inside contentHome will do the trick. Try reading this, maybe it will help you https://developer.android.com/training/basics/fragments/fragment-ui.html
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.