gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:19:21 25 4
gpt4 key购买 nike

我正在制作一个使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com