有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java片段没有显示其全部宽度和高度

我正在安卓应用程序中创建一个滑动菜单。但在运行时,我的片段中没有一个显示出“match_parent”设置的完整大小

**如图所示:**

enter image description here

这是我的代码

布局:

<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) 个答案

  1. # 1 楼答案

    检查片段onCreateView()方法

    你应该像这样膨胀视图:

    val root = inflater.inflate(R.layout.list_content,container, false);
    return root
    

    并确保在执行片段事务时添加片段的place_holder是可见的

    fragmentTransaction.replace(R.id.fragment_container, YourFragment())
    

    在上面的代码中,确保fragment_容器是可见的,并且具有适当的高度和宽度