片段与AppCompat工具栏重叠 [英] Fragment overlaps the AppCompat toolbar

查看:61
本文介绍了片段与AppCompat工具栏重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用v7支持库,并试图在左侧创建一个导航抽屉. 如我在其他地方所读过的,我设置了:

I'm working with the v7 support library and trying to have a navigation drawer on the left. As read elsewhere I set up:

  1. DrawerTest.java :保存抽屉的主要活动,我在其中加载了工具栏 使用setSupportActionBar(),来自自定义XML布局,该布局包含 只是Toolbar;

  1. DrawerTest.java: The main activity that holds the drawer, into which I load my Toolbar with setSupportActionBar(), from a custom XML layout that holds just the Toolbar;

toolbar.xml :包含工具栏的XML布局;

toolbar.xml: A XML layout holding the toolbar;

activity_drawer_listview.xml :一种DrawerLayout XML资源,用于保存我的片段的容器 (第2节中提到的F​​rameLayout <including>布局). 导航抽屉(一个ListView);

activity_drawer_listview.xml: A DrawerLayout XML resource, that holds containers for my fragment (a FrameLayout <including> the layout mentioned in 2.) and for the navigation drawer (a ListView);

FragmentTest.java :一些非常简单的片段代码,扩展了Fragment;

FragmentTest.java: Some really simple fragment code, extending Fragment;

我将在此处粘贴一些代码,无论如何,我的问题是片段布局似乎从屏幕的顶部开始,而不是从Toolbar的底部开始. 5.中输入的任何文本都将与操作栏上的应用程序标题重叠.我在哪里错了?

I'll paste some code here, anyway my problem is that the fragment layout seems to start from the top of the screen, and not from the bottom of the Toolbar. Any text put in 5. will overlap the app title on the action bar. Where am I wrong?

(1.) DrawerTest.java

    public class DrawerTest extends ActionBarCompat {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drawer_listview);

        DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        Toolbar tb = (Toolbar) findViewById(R.id.toolbar_main2);
        ActionBarDrawerToggle abDrawerToggle = new ActionBarDrawerToggle(
                        this, drawerLayout, tb,
                        R.string.navigation_drawer_open,
                        R.string.navigation_drawer_close )
        {
            // onDrawerClosed() { ... }
            // onDrawerOpened() { ... }
        };
        drawerLayout.setDrawerListener(abDrawerToggle);
        setSupportActionBar(tb);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        abDrawerToggle.syncState();

        //code to load my fragment
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.frame_layout_test, new FragmentTest()).commit();

        }
    }

(3.) activity_drawer_listview.xml

    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context="miav.ciotole.DrawerTest">

    <FrameLayout android:id="@+id/frame_layout_test" android:layout_width="match_parent"
        android:layout_height="match_parent" >
    <include layout="@layout/toolbar"/> <!-- What is this line about? -->
    </FrameLayout>

<ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>

(4.) FragmentTest.java

    public class FragmentTest extends Fragment {

    public FragmentTest() { }

    @Override
    public View onCreateView(LayoutInflater infl, ViewGroup container, Bundle SavedInstanceState) {
        View rootView = infl.inflate(R.layout.fragment_test_layout, container, false);
        return rootView;
    }
}

(5.) fragment_test_layout.xml

<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"
// padding ...
>

<TextView android:id="@+id/section_label" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world"/>

注意:我发现了一些问题(和答案),但是在大多数情况下,该问题与AppCompat版本< 19,这不是我的情况.

Note: I found some questions (and answers), but in most cases, the issue was related to AppCompat versions < 19, which is not my case.

注2:我正在从Theme.AppCompat.NoActionBar继承,因为我正在运行时设置工具栏.也许我可以解决从Theme.AppCompat继承的问题,并避免使用setSupportActionBar(),但是如果可能的话,我会保留实际配置,因为这样可以更轻松地控制ActionBar.

Note2: I am inheriting from Theme.AppCompat.NoActionBar, as I'm setting the toolbar on runtime. Probably I could solve inheriting from Theme.AppCompat and avoid using setSupportActionBar(), but if possible I would stay with the actual configuration, as it makes easier to control the ActionBar.

推荐答案

原因是因为您将其放置在框架布局中,然后将片段添加到工具栏的顶部.你需要做这样的事情

The reason is because you place it in a frame layout and then you add the fragment ontop of the toolbar. you need to do something like this

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

       <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical">

       <android.support.v7.widget.Toolbar
           android:id="@+id/toolbar"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:minHeight="?attr/actionBarSize"
           android:background="?attr/colorPrimary"
           app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
           app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

       <FrameLayout
           android:id="@+id/content_frame"
           android:layout_width="match_parent"
           android:layout_height="match_parent" />

       </LinearLayout>

       <FrameLayout
       android:id="@+id/left_drawer"
       android:layout_width="325dp"
       android:layout_height="match_parent"
       android:layout_gravity="start"
       android:background="#FFFFFF"/>

</android.support.v4.widget.DrawerLayout>

这篇关于片段与AppCompat工具栏重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆