小编Lea*_*roG的帖子

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

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

居中,没有汉堡 偏离中心

这是我的主要布局

<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>
Run Code Online (Sandbox Code Playgroud)

这是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>
Run Code Online (Sandbox Code Playgroud)

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

    toolbar = (Toolbar) findViewById(R.id.atiToolBar);
    if (toolbar …
Run Code Online (Sandbox Code Playgroud)

android android-layout

5
推荐指数
1
解决办法
899
查看次数

标签 统计

android ×1

android-layout ×1