简体   繁体   中英

App Theme(Actionbar) is not respecting your space

I'm using a Navigation Drawer , but every time I put something in the fragment, the action bar does not respect the layout.

The image itself explains:

在此处输入图片说明

The fragment_main :

<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="com.example.leonel.picollo.MainFragment">

<Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="MAIN"
   android:padding="8dp"
   android:textColor="#fff"
   android:background="@color/colorPrimary"
   android:textSize="28sp"
   android:id="@+id/textView"
   android:layout_centerHorizontal="true"
   android:layout_centerVertical="true"
/>

</RelativeLayout>

And the MainFragment.java :

package com.example.leonel.picollo;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class MainFragment extends Fragment {


public MainFragment() {
    // 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, container, false);
}

}

Using custom toolbar has some consequences. Custom toolbar has height and other elements don't respect it in default, we must set them padding or margin. Add to RelativeLayout marginTop on height of Toolbar.

<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"
android:marginTop="?attr/actionBarSize" <!-- here is marginTop -->
tools:context="com.example.leonel.picollo.MainFragment">
</RelativeLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM