android在片段中隐藏标题

use*_*293 8 android android-fragments

我已经开始在片段中工作。我试图隐藏 titleBar在片段中,但我有这个 Log-cat 错误

android fragment requestfeature must be called before adding content
Run Code Online (Sandbox Code Playgroud)

这是一个来源

public class SendItemsFragment extends Fragment {

Button b1;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    getActivity().requestWindowFeature(Window.FEATURE_NO_TITLE);


    View rootView = inflater.inflate(R.layout.send_items, container, false);



    return rootView;
    }

}
Run Code Online (Sandbox Code Playgroud)

小智 8

这对我有用:

隐藏操作栏

((AppCompatActivity) getActivity()).getSupportActionBar().hide();
Run Code Online (Sandbox Code Playgroud)


小智 1

只需将您的代码更改为

requestWindowFeature(Window.FEATURE_NO_TITLE);
Run Code Online (Sandbox Code Playgroud)

onCreate功能上而不是,

getActivity().requestWindowFeature(Window.FEATURE_NO_TITLE);

public class SendItemsFragment extends Fragment {
   private Activity activity;
   public SendItemsFragment (Activity act) {
       this.activity = act;
   }

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         activity.requestWindowFeature(Window.FEATURE_NO_TITLE);
         View rootView = inflater.inflate(R.layout.send_items, container, false);
         return rootView;
   }
}
Run Code Online (Sandbox Code Playgroud)