片段重叠我AppCompat工具栏 [英] Fragment overlaps my AppCompat toolbar

查看:149
本文介绍了片段重叠我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布局保存 在工具栏;

  1. DrawerTest.java: A 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 :A DrawerLayout XML资源,保存容器我的片段 (一的FrameLayout <包括> 2中提到的布局),并为 抽屉式导航栏(一个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 :一些很简单的片段code,延长片段;

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

我会贴上一些code在这里,反正我的问题是,该片段布局似乎从屏幕顶部的开始,而不是从工具栏底部。放在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版本&LT; 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(),但如果可能的话我会留在实际配置,因为它使更容易控制的动作条。

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.

推荐答案

究其原因是因为你把它放在一个框架布局,然后添加片段ontop工具栏。你需要做这样的事情

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天全站免登陆