android - 抽屉图标(汉堡)将工具栏图标移离中心

标签 android android-layout

我正在使用带有 DrawerLayout 的工具栏。我的工具栏有 2 个 View (按钮),一个在中间,一个靠近右边距。 当我使用 getSupportActionBar().setDisplayHomeAsUpEnabled(true) 打开汉堡图标时,我的中央按钮稍微向右移动。
我通过根据工具栏宽度计算左边距,减去汉堡图标大小和我的图标大小的 1/2,设法使按钮居中。我正在寻找更好的解决方案,因为这可能不适用于所有密度和屏幕尺寸。

centered, no burger off center

这是我的主要布局

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:fitsSystemWindows="false"
    android:layout_height="match_parent">
    <!-- The main content view -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <include layout="@layout/ati_toolbar"/>

        <FrameLayout android:id="@+id/main_container"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_height="match_parent" />

    </LinearLayout>
    <!-- The navigation drawer -->
    <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:background="#111"/>
</android.support.v4.widget.DrawerLayout>

这是 ati_toolbar 布局

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/atiToolBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Toolbar_Theme">
<RelativeLayout
    android:id="@+id/menuBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false">
        <ImageButton
            android:id="@+id/btnask"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:src="@drawable/ask_button"
            android:background="@color/ATIBlack"
            />

    <EditText
        android:id="@+id/searchBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="SEARCH"
        android:layout_marginRight="20dp"
        android:textSize="15dp"
        android:background="@color/background_material_light" />
</RelativeLayout>

主要 Activity 中的工具栏和抽屉设置

    toolbar = (Toolbar) findViewById(R.id.atiToolBar);
    if (toolbar != null) {
        setSupportActionBar(toolbar);   

    }
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);  



    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);  
    mDrawerList = (ListView) findViewById(R.id.left_drawer);  
    List<Category> cList = null;
    _cAdapter = new CategoryListAdapter(cList,getBaseContext());
    mDrawerList.setAdapter(_cAdapter);
    CategoryService cs = new CategoryService(getBaseContext(), _cAdapter);
    cs.load();

    mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); // Listens to clicks on categories

    // Drawer Toggle management
    mDrawerToggle = new ActionBarDrawerToggle(
            this,                  /* host Activity */
            mDrawerLayout,         /* DrawerLayout object */
             //  toolbar,
            R.string.drawer_open,  /* "open drawer" description */
            R.string.drawer_close  /* "close drawer" description */
    );
    mDrawerLayout.setDrawerListener(mDrawerToggle);

最佳答案

在工具栏上使用 app:contentInsetStartWithNavigation="0dp"。 希望这会有所帮助。

关于android - 抽屉图标(汉堡)将工具栏图标移离中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31812008/

相关文章:

android - 具有旋转 View 的约束布局

Android WiFi 设备到 AP 的往返时间 (RTT)

java - 在android中请求最近的城市

android - 你能用我的代码找出错误吗?

java - 如何隐藏 View 后面的 View

android - 使用 DataBinding 的 <include> 标记的 java.lang.ClassCastException

android - ApplicationStatus类E/SysUtils:ApplicationStatus中的ApplicationContext为null

android - 为什么我不能创建日历?

java - 同一 View 上的两个独立的可滚动 fragment - Android

android - Android 键盘关闭时闪烁?