小编Bra*_*ani的帖子

操作栏在用片段替换时向下移动

我正在为活动添加一个片段,然后我用第二个片段替换该片段.替换第二个片段后,操作栏稍微向下移动 在此输入图像描述

我的活动代码

public class MyActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.meetinglist_activity);
    Fragment first_fragment = new FirstFragment();
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.add(R.id.content_frame, first_fragment).commit();
     }
Run Code Online (Sandbox Code Playgroud)

在第一个片段的按钮单击中替换第二个片段

public class Firstfragment extends Fragment  {


FloatingActionButton new_fab;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View list_view=inflater.inflate(R.layout.meetingslist_fragment,container,false);
    new_fab=(FloatingActionButton)list_view.findViewById(R.id.meeting_new);
    new_fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Fragment second_fragment = new SecondFragment();
            FragmentTransaction ft =getActivity().getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.content_frame, second_fragment).commit();
  }
Run Code Online (Sandbox Code Playgroud)

活动布局

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="match_parent"
 android:layout_height="match_parent"> …
Run Code Online (Sandbox Code Playgroud)

android fragment android-fragments android-actionbar

5
推荐指数
2
解决办法
1169
查看次数