我正在我的 Android 应用程序中创建一个滑动菜单。但在运行时,我的 fragment 都没有显示“match_parent”设置的完整大小。
**这是图片:**
这是我的代码
布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="@android:color/holo_red_light"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android: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;
}
最佳答案
检查您的 fragment onCreateView()
方法
你应该像这样膨胀 View :
val root = inflater.inflate(R.layout.list_content,container, false);
return root
并确保在进行 fragment 交易时添加 fragment 的 place_holder
可见
fragmentTransaction.replace(R.id.fragment_container, YourFragment())
在上面的代码中确保fragment_container可见并且具有适当的高度和宽度
关于java - fragment 未显示其完整宽度和高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56862987/