Android FragmentTransaction fragment不';我没有出现
我想在onCreate方法中添加片段。但是什么也没有出现。我没有发现任何错误。 我尝试在.commit之后使用executePendingTransactions(),但没有任何更改。我还尝试了fragment_容器的LinearLayout和RelativeLayout,而不是FrameLayout。怎么了 MainActivity.javaAndroid FragmentTransaction fragment不';我没有出现,android,fragment,Android,Fragment,我想在onCreate方法中添加片段。但是什么也没有出现。我没有发现任何错误。 我尝试在.commit之后使用executePendingTransactions(),但没有任何更改。我还尝试了fragment_容器的LinearLayout和RelativeLayout,而不是FrameLayout。怎么了 MainActivity.java public class MainActivity extends AppCompatActivity implements Navig
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
NavigationView navigationView = null;
Toolbar toolbar = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MainActivityFragment fragment = new MainActivityFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
...
}
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class MainActivityFragment extends Fragment {
public MainActivityFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_main_activity, container, false);
}
}
activity_main.xml
<?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:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
在framelayout中使用
app:layout\u behavior=“@string/appbar\u scrolling\u view\u behavior”
正如秘书长所解释的:
设计库将其提升到下一个级别:使用AppBarLayout
允许您的工具栏和其他视图(例如
TabLayout)对标记为的同级视图中的滚动事件作出反应
滚动视图行为
在framelayout中使用
app:layout\u behavior=“@string/appbar\u scrolling\u view\u behavior”
正如秘书长所解释的:
设计库将其提升到下一个级别:使用AppBarLayout
允许您的工具栏和其他视图(例如
TabLayout)对标记为的同级视图中的滚动事件作出反应
滚动视图行为
在FrameLayout中使用app:layout\u behavior=“@string/appbar\u scrolling\u view\u behavior”帮助,非常感谢:)问题已解决。在FrameLayout中使用app:layout\u behavior=“@string/appbar\u scrolling\u view\u behavior”帮助,非常感谢:)问题已解决。捕捉不错,希望你不介意,但我添加了一点解释,说明了它的来源和原因,并链接到了dev博客。很好的捕获,希望你不介意,但我添加了一点解释,说明了它的来源和原因,并链接到dev博客。
<RelativeLayout 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"
tools:context="pl.example.trainingapp.MainActivityFragment">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MainActivityFragment!" />
</RelativeLayout>
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class MainActivityFragment extends Fragment {
public MainActivityFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_main_activity, container, false);
}
}