java - 尽管添加到 View 中, fragment 仍未显示

标签 java android android-studio fragment

我正在制作一个使用 fragment 的应用程序,我的问题是尽管 fragment 已成功添加到 View 中,但它没有显示在我的代码中。

我做错了什么?

Here is my main activity where I am adding the fragments

    import android.annotation.TargetApi;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.os.Build;
    import android.os.Bundle;
    import android.support.annotation.RequiresApi;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v4.app.FragmentTransaction;
    import android.view.View;
    import android.support.design.widget.NavigationView;
    import android.support.v4.view.GravityCompat;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.ActionBarDrawerToggle;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.Menu;
    import android.view.MenuItem;

    public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {
MenuItem menuSettings;
//Fragments variables
DictionaryFragment dictionaryFragment;
BookmarkFragment bookmarkFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    // new dictionary objects
    dictionaryFragment = new DictionaryFragment();
    bookmarkFragment = new BookmarkFragment();
    // Call the gotoFragment method and pass the dictionaryFragment as the default ontop parameter
    gotToFragment(dictionaryFragment, true);
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    menuSettings = menu.findItem(R.id.action_settings);

   String id = Global.getState(this,"dic_type");
   // Handles the null value on first run
   if (id !=null){
       onOptionsItemSelected(menu.findItem(Integer.valueOf(id)));
   }
    return true;
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    Global.saveState(this,"dic_type",String.valueOf(id));

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_engluo) {
        menuSettings.setIcon(getDrawable(R.drawable.eng_luo));

    }
    else if(id == R.id.action_luoeng){
        menuSettings.setIcon(getDrawable(R.drawable.luo_eng));

    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_bookmark) {
        gotToFragment(bookmarkFragment, false);
    } else if (id == R.id.nav_rate) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_help) {

    } else if (id == R.id.nav_about) {

   // } else if (id == R.id.nav_send) {

    }

    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

void gotToFragment(Fragment fragment, boolean isTop){
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.fragment_container, fragment);
    if(!isTop){
        fragmentTransaction.addToBackStack(null);
    }
    fragmentTransaction.commit();
    }
}

如您所见,我已将它们添加到 fragment_container,它是我的 app_bar_main.xml 文件中的 FrameLayout。

Here is the main activity code

     <?xml version="1.0" encoding="utf-8"?>
     <android.support.v4.widget.DrawerLayout 
     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:id="@+id/drawer_layout"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">
    <include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
    <android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:menu="@menu/activity_main_drawer" />
   </android.support.v4.widget.DrawerLayout>

And this is the code for the app_bar_main

    <?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:contentInsetStartWithNavigation="0dp">

        <EditText
            android:id="@+id/edit_search"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@drawable/radius_edit_text"
            android:drawableLeft="@drawable/ic_search"
            android:textColor="@color/colorBlack"
            android:textColorHint="@color/colorGrey"
            android:hint="@string/search"
            android:textSize="20sp"
            android:paddingLeft="5dp"
            android:drawablePadding="5dp"
            android:inputType=""
            android:drawableStart="@drawable/ic_search"
            android:paddingStart="5dp" />
            </android.support.v7.widget.Toolbar>

           </android.support.design.widget.AppBarLayout>

       <FrameLayout
       android:id="@+id/fragment_container"
       android:layout_width="match_parent"
      android:layout_height="match_parent">
     </FrameLayout>
    </LinearLayout>

我做错了什么? 谢谢

最佳答案

您的线性布局是水平的。您的框架布局可能已被推离屏幕

关于java - 尽管添加到 View 中, fragment 仍未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50296065/

相关文章:

android - Dagger2 和 Kotlin 运行失败,原因是 :app:compileDebugKotlinAfterJava

android - 如何在 Retrofit 中解析没有任何对象的单个元素的 JSON 数组?

android - 强制 Android Studio 显示灯泡

android - Gradle找不到依赖

android - 如何在android studio中从数据库中取消文本转义

java - 在 ubuntu 中设置 java_home 时遇到问题

java - 外部 jquery 文件在 spring MVC 中不起作用?

android - 如果已经从 Android 中的 FB SDK 获得访问 token ,如何在 WebView 中登录 facebook

java - 如何使用 JiBX 读取 XML 文档?

java - 如何在Spring RestTemplate中发送调用者的IP地址