【发布时间】:2014-12-23 17:06:06
【问题描述】:
我正在使用 v7 支持库并试图在左侧有一个导航抽屉。 正如我在其他地方读到的那样:
DrawerTest.java:存放抽屉的主要活动,我将工具栏加载到其中 使用
setSupportActionBar(),来自自定义 XML 布局,该布局包含 只是Toolbar;toolbar.xml:包含工具栏的 XML 布局;
activity_drawer_listview.xml:一个
DrawerLayoutXML 资源,用于保存我的片段的容器 (一个 FrameLayout<including>2 中提到的布局。)和 导航抽屉(一个 ListView);FragmentTest.java:一些非常简单的片段代码,扩展
Fragment;- fragment_test_layout.xml:一些非常简单的片段布局,里面只有一个 TextView。
我会在这里粘贴一些代码,无论如何我的问题是片段布局似乎从屏幕顶部开始,而不是从Toolbar 的底部开始。 5. 中的任何文本都将与操作栏上的应用程序标题重叠。我哪里错了?
(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"/>
注意:我发现了一些问题(和答案),但在大多数情况下,问题与
注意 2:我从 Theme.AppCompat.NoActionBar 继承,因为我在运行时设置工具栏。可能我可以解决从 Theme.AppCompat 继承的问题并避免使用setSupportActionBar(),但如果可能的话,我会保留实际配置,因为这样更容易控制 ActionBar。
【问题讨论】:
标签: android android-fragments android-toolbar