片段重叠我的程序兼容性工具栏

问题描述 投票:31回答:5

我与V7支持库的工作,并试图对左边抽屉式导航。至于其他地方读我设置:

  1. DrawerTest.java:持有抽屉,到了我加载我的工具栏与setSupportActionBar(),从保持刚刚Toolbar自定义XML布局中的主要活动;
  2. toolbar.xml:一个XML布局保持的工具栏;
  3. activity_drawer_listview.xml:甲DrawerLayout XML资源,保持容器为我的片段(一个的FrameLayout <including>在2.中提到的布局)和用于导航抽屉(一个ListView);
  4. FragmentTest.java:一些非常简单的代码片段,延长Fragment;
  5. fragment_test_layout.xml:一些非常简单的片段布局,只是一个内部的TextView。

我会在这里粘贴一些代码,反正我的问题是,该片段布局似乎从屏幕顶部开始,而不是从Toolbar的底部。放在5.任何文本将重叠操作栏上的应用程序标题。我在哪里错了?

(1.) draw而test.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.) fragment test.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"/>

注:我发现了一些问题(和答案),但在大多数情况下,问题涉及到程序兼容性版本<19,这不是我的情况。

注2:我从Theme.AppCompat.NoActionBar继承,因为我设置上运行时工具栏。也许我可以解决从Theme.AppCompat继承和避免使用setSupportActionBar(),但如果可能的话我会留在实际配置,因为这使得更容易控制的动作条。

android android-fragments android-toolbar
5个回答
60
投票

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

<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>

6
投票

加入这一行你的FrameLayout

app:layout_behavior="@string/appbar_scrolling_view_behavior"

1
投票

你把相同的FrameLayout工具栏(用id = frame_layout_test)。的FrameLayout重叠的看法。

我猜你正在尝试做这样的事情:

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" 
android:layout_height="match_parent"
android:orientation="vertical" >

<include layout="@layout/toolbar"/> 
<!-- I don't know what your Toolbar layout is -->


    <android.support.v4.widget.DrawerLayout 
        android:layout_width="match_parent" 
        android:layout_height="0dp"
        android:layout_weight="1" 
        tools:context="miav.ciotole.DrawerTest">

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

        <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>
</LinearLayout>

从上面的布局采用的线性布局和对齐的FrameLayout(在那里你会虚增您framgemt)在工具栏下面...

该线

android:layout_height="0dp"
android:layout_weight="1"

说,DrawerLayout应该采取的工具栏下的剩余高度。

不过,如果你想显示一个传统的动作条/工具栏,你不必在XML布局添加一个工具栏。您只需可以更改活动的主题,以@style/Theme.AppCompat.Light.DarkActionBar


0
投票

这是非常简单和容易。只要按照我试过下面说。

通过使用更换任何观点:

**

getFragmentManager().beginTransaction()
                .replace(R.id.blankFragment, new SettingsFragment())
                .commit();

** //这里,blackFragment是的FrameLayout视图ID。你有碎片的布局替换FrameLayout里查看。注:应该是的FrameLayout或FrameLayout里的衍生品布局。

我的整个代码是:

1) settings activity.Java

**

public class SettingsActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);
        Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar2);
        mToolbar.setTitle("Settings");
////        remove the left margin from the logo
        mToolbar.setPadding(2, 0, 0, 0);//for tab otherwise give space in tab
        mToolbar.setContentInsetsAbsolute(0, 0);
        setSupportActionBar(mToolbar);
        // Display the fragment as the main content
        getFragmentManager().beginTransaction()
                .replace(R.id.blankFragment, new SettingsFragment())
                .commit();
    }
}

**

2)activity_fragment.xml

**

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="horizontal">
    <!--scroll|snap-->
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/statusbar"
        android:minHeight="?attr/actionBarSize"
        app:theme="@style/ThemeOverlay.AppCompat.ActionBar" />
    <FrameLayout
        android:id="@+id/blankFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="?attr/actionBarSize"
        android:layout_gravity="top"
        android:fitsSystemWindows="true"
        android:orientation="horizontal" />
</FrameLayout>

**你可以看到我的屏幕后,我替换片段的观点的FrameLayout的观点

enter image description here


0
投票

我有两个活动,并附加一个片段给他们。在第一活动中,示出右,而在第二它重叠工具栏。虽然在片段的布局android:layout_marginTop="?android:attr/actionBarSize"可以是溶液,我发现了一个错误。

由于科特林缓存的看法布局的IDS(而不是findViewById(R.id.some_id)你可以写some_id),还有一个很大的问题。每次复制一个活动或片段时,你应该非常小心。所以,每一次检查的进口和连接布局。

我复制到FirstActivity SecondActivity,复制activity_first到activity_second,却忘了更改:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_first) // Don't forget to change.

同样,在一个片段不要忘记:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {

    val view = inflater.inflate(R.layout.fragment_yours, container, false)

所以,在我进口我有:

import kotlinx.android.synthetic.main.activity_second.*

在这种情况下toolbar参考activity_second,而activity_first竟是连接。然后将片段上移动 - 就在工具栏下方。在大多数情况下,科特林从未显示了编译或运行时期间缓存ids中的任何问题。这是一个很大的痛苦。

© www.soinside.com 2019 - 2024. All rights reserved.