gpt4 book ai didi

java - android studio 如何将导航 Activity 中的 fragment 设置为启动页面

转载 作者:太空宇宙 更新时间:2023-11-04 11:53:01 27 4
gpt4 key购买 nike

目前我使用fragment_empty作为启动。 fragment_empty我想要fragment_home作为启动页面(当你启动应用程序时),但我不想要 fragment_home在每个页面上都可以看到。

fragment_home 页面:

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welkom op home!"
android:padding="10dp"
/>

<ImageView
android:layout_width="236dp"
android:layout_height="218dp"
android:src="@mipmap/ic_launcher"
android:id="@+id/imageView2" />

fragment_empty页面:

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

我的 MainActivity(抽屉导航):

public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Fragment fragment = null;
if (id == R.id.nav_home) {
fragment = new HomeFragment();
} else if (id == R.id.nav_evenementen) {
fragment = new EvenementenFragment();
} else if (id == R.id.nav_contact) {
fragment = new ContactFragment();
} else if (id == R.id.nav_share) {

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

}


if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();

}

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

App_bar_主页:

<include layout="@layout/fragment_empty" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />

<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>

最佳答案

在 MainActivity onCreate() 方法中,您可以直接调用第一次 HomeFragment,例如

HomeFragment mHomeFragment = new HomeFragment();

FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, mHomeFragment).commit();

关于java - android studio 如何将导航 Activity 中的 fragment 设置为启动页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41613497/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com