【问题标题】:Android fragment is not displayed, even after inflating itAndroid片段不显示,即使在膨胀之后
【发布时间】:2021-03-20 17:08:10
【问题描述】:

我声明我是应用程序开发的初学者,尤其是片段。
我正在 AndroidStudio 中开发一个应用程序项目;我正在尝试制作一个显示在线状态、收到的消息等信息的主页。
对于每条信息,我都尝试关联一个片段,但没有显示,即使代码中没有错误。
我使用的是 4.2 版本,我用 Java 编写代码;我已经尝试了一切,但我已经完成了想法。
有什么建议?我错过了什么吗?I was doing something like the image shown in this link.
我将在下面发布代码:

这是MainActivity:

public class MainActivity extends AppCompatActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BottomNavigationView buttonNV= findViewById(R.id.menu);
        buttonNV.setOnNavigationItemSelectedListener(navListener);


        getSupportFragmentManager().beginTransaction().add(R.id.online_frame,new OnlineStatusFragment()).commit();

    }

online_fragment(我的片段布局):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="203dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="42dp"
        android:layout_marginLeft="42dp"
        android:layout_marginTop="36dp"
        android:layout_marginEnd="27dp"
        android:layout_marginRight="27dp"
        android:layout_marginBottom="277dp"
        android:background="@drawable/border_tv"
        android:text="SEI ONLINE/TESTING"
        app:drawableRightCompat="@drawable/settings_icon" />
</RelativeLayout>

activity_main.xml:
<RelativeLayout 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"
    android:background="@color/background">

    <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="150dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="55dp"
            android:layout_alignParentLeft="true"
            android:id="@+id/online_frame">

        <fragment
            android:id="@+id/online_fragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.example.progetto_lso_b.OnlineStatusFragment"
            tools:layout="@layout/online_fragment" />

    </FrameLayout>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:menu="@menu/bottom_menu"
        android:layout_alignParentBottom="true"
        android:background="?android:attr/windowBackground"
        android:id="@+id/menu"/>

</RelativeLayout>

这是 OnlineStatusFragment.java:
public class OnlineStatusFragment extends Fragment {

    public OnlineStatusFragment() {}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate il layout per questo fragment
        return inflater.inflate(R.layout.online_fragment, container, false);
    }
}

【问题讨论】:

  • 只需更改 Framelayout 高度。 android:layout_width="match_parent" android:layout_height="wrap_content"
  • 而你这样做的方式,它会添加2个在线片段。 1 个来自活动代码(使用 getSupportFragmentManager)和 1 个来自 xml 代码( 标签)
  • 那么,我应该从 XML 代码中删除片段吗? @KishanMaurya
  • 这是您的选择。要么会工作。我更喜欢使用主要活动之一。它将为片段提供更多控制,例如添加/替换
  • 在这种情况下,您必须创建 3 个容器,即。 3个片段的FrameLayout并在各自的容器中添加片段

标签: java android xml android-studio fragment


【解决方案1】:

我检查了您的代码,我认为这是因为您没有将 FrameLayout 添加到 Fragment。你可以尝试在 online_fragment.xml 中这样做吗:

    <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=".fragment.OnlineFragment">
    
    
    <RelativeLayout  android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="203dp"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginStart="42dp"
            android:layout_marginLeft="42dp"
            android:layout_marginTop="36dp"
            android:layout_marginEnd="27dp"
            android:layout_marginRight="27dp"
            android:layout_marginBottom="277dp"
            android:background="@drawable/border_tv"
            android:text="SEI ONLINE/TESTING"
            app:drawableRightCompat="@drawable/settings_icon" />
    </RelativeLayout>

</FrameLayout>

【讨论】:

  • 我试过了,但没有任何改变;它没有给出任何错误。我认为问题出在布局文件中;也许我错过了一些东西或比平时多放了一些东西
【解决方案2】:

我发现我在代码中做错了什么。
我以错误的方式设置了 TextViews 的宽度和高度;事实上,我不得不分别用match_parent 和wrap_content 设置这些数据。
此外,我删除了文件 activity_main.xml 中的&lt;fragment&gt;(就像之前有人评论过的那样),因为它会“复制”片段。
这是 online_fragment.xml 中的代码:

<RelativeLayout 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=".OnlineStatusFragment">

    <TextView
        android:id="@+id/online_status"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="0dp"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="1dp"
        android:text="Sei online/sei offline"
        android:drawableRight="@drawable/settings_icon"/>

</RelativeLayout>

这是 activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:background="@color/background">

    <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="150dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="55dp"
            android:layout_alignParentLeft="true"
            android:id="@+id/online_frame">
    </FrameLayout>
    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:menu="@menu/bottom_menu"
        android:layout_alignParentBottom="true"
        android:background="?android:attr/windowBackground"
        android:id="@+id/menu"/>

</RelativeLayout>

感谢大家帮助我!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-16
    • 1970-01-01
    • 1970-01-01
    • 2011-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多