java片段没有显示其全部宽度和高度
我正在安卓应用程序中创建一个滑动菜单。但在运行时,我的片段中没有一个显示出“match_parent”设置的完整大小
**如图所示:**
这是我的代码
布局:
<FrameLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:background="@安卓:color/holo_red_light"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent">
<TextView
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:text="@string/hello_blank_fragment" />
</FrameLayout>
Java代码:
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.nav_place_order:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new PlaceOrderPage1Fragment()).commit();
break;
case R.id.nav_profile:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new ProfileFragment()).commit();
break;
case R.id.nav_settings:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new SettingsFragment()).commit();
break;
case R.id.sample:
Toast.makeText(this,"Sample",Toast.LENGTH_SHORT).show();
break;
}
# 1 楼答案
检查片段
onCreateView()
方法你应该像这样膨胀视图:
并确保在执行片段事务时添加片段的
place_holder
是可见的在上面的代码中,确保fragment_容器是可见的,并且具有适当的高度和宽度