Fragment无法加载,显示空白

现在问题如图
现在问题如图

下面的四个导航,每一个应该能对应加载一个Fragment,但是全部加载不出来
切换文字没有问题,也用debugger检查过了,可以确定bottombar的listener是正常的,代码可以执行

关键代码

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

android:id="@+id/container"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

<LinearLayout

android:id="@+id/content"

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="1"

android:orientation="horizontal">

//<Textview/> 省略

<FrameLayout

android:layout_width="match_parent"

android:layout_height="fill_parent"

android:id="@+id/main_content">

</FrameLayout>

</LinearLayout>

<com.roughike.bottombar.BottomBar

android:id="@+id/bottomBar"

android:layout_width="match_parent"

android:layout_height="60dp"

android:layout_alignParentBottom="true"

app:bb_tabXmlResource="@xml/bottombar_tabs" />

</LinearLayout>

activity_main.java

import android.app.FragmentManager;

import android.app.FragmentTransaction;

import android.app.Fragment;

public class MainActivity extends AppCompatActivity{

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mTextMessage = (TextView) findViewById(R.id.message);

BottomBar bottomBar = (BottomBar) findViewById(R.id.bottomBar);

bottomBar.setOnTabSelectListener(new OnTabSelectListener() {

@Override

public void onTabSelected(@IdRes int tabId) {

FragmentManager fm=getFragmentManager();

FragmentTransaction ft=fm.beginTransaction();

switch(tabId) {//四个都无法加载

case R.id.tab_home: {

mTextMessage.setText(R.string.title_home);

mHome = new HomeFragment();

ft.replace(R.id.main_content,mHome);

break;

}

case R.id.tab_dashboard: {

mTextMessage.setText(R.string.title_dashboard);

mDashboard = new DashboardFragment();

ft.replace(R.id.main_content, mDashboard);

break;

}

case R.id.tab_bbs: {

mTextMessage.setText(R.string.title_bbs);

mBBS = new BBSFragment();

ft.replace(R.id.main_content, mBBS);

break;

}

case R.id.tab_profile: {

mTextMessage.setText(R.string.title_profile);

mProfile = new ProfileFragment();

ft.replace(R.id.main_content, mProfile);

break;

}

default:{

Toast.makeText(getApplicationContext(),R.string.error_nulltab,Toast.LENGTH_SHORT).show();

}

}

ft.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE);

ft.addToBackStack(null);

ft.commitAllowingStateLoss();

}

});

loadFragmentHome();

}

//这个也加载不出来

public void loadFragmentHome(){

FragmentManager fm=getFragmentManager();

FragmentTransaction ft=fm.beginTransaction();

mHome=new HomeFragment();

ft.replace(R.id.main_content,mHome);

ft.commitAllowingStateLoss();

}

//其它省略

}

fragment_home.xml(随便举个例子,其它结构类似)

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout 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">

<TextView

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="@string/title_Home" />

</FrameLayout>

HomeFragment.java

import android.app.Fragment;

public class HomeFragment extends Fragment{

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

// Inflate the layout for this fragment

return inflater.inflate(R.layout.fragment_home, container, false);

}

//其它省略

}

已经尝试过将app.Fragment替换成support.v4.app.Fragment,无效
尝试过将MainActivity改为继承Activity或者FragmentActivity,皆无效

新手,轻喷

回答:

clipboard.png
将main_content的父LinearLayout方向改为orientation="vertical"试试

以上是 Fragment无法加载,显示空白 的全部内容, 来源链接: utcz.com/p/176296.html

回到顶部