使用导航组件时片段不显示
Fragment doesn't show up when using Navigation component
我正在尝试构建一个基于片段和导航组件的应用程序。下面的示例代码是我认为的绝对最小值。
我的 activity 布局如下所示:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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"
tools:context=".MainActivity">
<fragment
android:id="@+id/fragment_host"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
</FrameLayout>
对应的activityclass如下所示。请注意,到目前为止我还没有使用任何工具栏、底部导航或菜单。
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
要显示的起始片段只是一个 ConstraintLayout,它定义了一些常规按钮和一个浮动操作按钮。
该片段的实现如下所示:
public class FragmentMainScreen extends Fragment
{
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment_main_screen, container, false);
}
}
最后,导航图是这样的:
<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/nav_graph"
app:startDestination="@id/destination_main">
<fragment
android:id="@+id/destination_main"
android:name="com.stmoebius.zz.ui.FragmentMainScreen"
android:label="@string/main_screen_title"
tools:layout="@layout/fragment_main_screen" />
</navigation>
一切构建和执行都很好,但片段没有显示(除了它的标签)。我错过了什么?
您将片段宽度和高度设置为 0dp
<FrameLayout
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"
tools:context=".MainActivity">
<fragment
android:id="@+id/fragment_host"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
</FrameLayout>
我正在尝试构建一个基于片段和导航组件的应用程序。下面的示例代码是我认为的绝对最小值。
我的 activity 布局如下所示:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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"
tools:context=".MainActivity">
<fragment
android:id="@+id/fragment_host"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
</FrameLayout>
对应的activityclass如下所示。请注意,到目前为止我还没有使用任何工具栏、底部导航或菜单。
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
要显示的起始片段只是一个 ConstraintLayout,它定义了一些常规按钮和一个浮动操作按钮。
该片段的实现如下所示:
public class FragmentMainScreen extends Fragment
{
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment_main_screen, container, false);
}
}
最后,导航图是这样的:
<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/nav_graph"
app:startDestination="@id/destination_main">
<fragment
android:id="@+id/destination_main"
android:name="com.stmoebius.zz.ui.FragmentMainScreen"
android:label="@string/main_screen_title"
tools:layout="@layout/fragment_main_screen" />
</navigation>
一切构建和执行都很好,但片段没有显示(除了它的标签)。我错过了什么?
您将片段宽度和高度设置为 0dp
<FrameLayout
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"
tools:context=".MainActivity">
<fragment
android:id="@+id/fragment_host"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
</FrameLayout>