【发布时间】: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