[英]Fragment not showing up despite being added to view
我正在制作一個使用片段的應用程序,我的問題是,盡管片段已成功添加到視圖中,但它並未顯示在我的代碼中。
我可能做錯了什么?
這是我要添加片段的主要活動
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();
}
}
如您所見,我將它們添加到了我的app_bar_main.xml文件中的FrameLayout的fragment_container中。
這是主要的活動代碼
<?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>
這是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>
我可能做錯了什么? 謝謝
您的線性布局是水平的。 您的框架布局可能已被推離屏幕
由於您的布局是水平的,因此您的片段將被推到看不見的位置,並且將不可見。因此,最好將其替換為垂直。 也建議更換
fragmentTransaction.add(R.id.fragment_container, fragment);
與
fragmentTransaction.replace(R.id.fragment_container, fragment);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.