自定义工具栏未正确设置

时间:2015-11-10 06:53:04

标签: android

在我的第二个活动中,默认工具栏正在某处设置,我无法弄明白。

second_activity.xml:

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

    <include
    android:id="@+id/tool_bar"
    layout="@layout/toolbar"
    ></include>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/user_posts"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        </ListView>

    </LinearLayout>
<ListView android:id="@+id/left_drawer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:layout_below="@id/user_profile"
            android:choiceMode="singleChoice"
            android:divider="@android:color/black"
            android:dividerHeight="1dp"
            android:layout_marginLeft="12dp"/>
</android.support.v4.widget.DrawerLayout>

预览此布局:

enter image description here

黑暗部分是我的自定义布局。

main_activity.xml

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <include
        android:id="@+id/tool_bar"
        layout="@layout/toolbar"
        ></include>
</android.support.design.widget.CoordinatorLayout>

预览main_activity.xml:

enter image description here

我的自定义工具栏在主要活动中正常运行。

Android.manifest:

<application
        android:name=".NetworkCalls"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme.NoActionBar" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".DashboardActivity"
            android:screenOrientation="portrait"/>
</application>

我已定义主题,在应用程序级别没有操作栏。

最奇怪的部分是当我在实际手机上运行时,我的第二个活动中没有出现工具栏,我的自定义工具栏会占用屏幕上的所有空间。

toolbar.xml:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:background="@drawable/bg_nav_bar"
        android:elevation="4dp">

    </android.support.v7.widget.Toolbar>

我非常感谢任何帮助。

修改:删除了多余的部分

public class DashboardActivity extends AppCompatActivity implements Serializable {
   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dashboard_activity);

        setupDrawerList();
        setupDashboardListAdapter();
    }
}

2 个答案:

答案 0 :(得分:1)

抽屉布局应设置类似于:

<DrawerLayout>
    <LinearLayout>
        <ToolBar/>
        <ListView/>
    </LinearLayout>
    <ListView/>
</DrawerLayout>

不确定这是否可以解决您的问题,请发布您的活动onCreateView()

答案 1 :(得分:-1)

这是因为它是标题栏而不是操作栏,这就是你隐藏它的方式:

//this line is to remove the title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);

//this line removes the notification bar (where the time is)
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

//you must set the content view after the above settings
this.setContentView(R.layout.your_layout_name_here);
相关问题