android 侧边栏菜单图标和文本未正确对齐

android sidebar menu icon and text not align properly

在上图中清楚地显示了文本和图标没有正确对齐所以如何正确对齐文本和图像。

列表视图代码

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

    <!-- Framelayout to display Fragments -->
    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- Listview to display slider menu -->
    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"        
        android:listSelector="@drawable/list_selector"
        android:background="@color/list_background"/>
</android.support.v4.widget.DrawerLayout>

做这样的事情

 <TextView
    android:id="@+id/home"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:drawableLeft="@mipmap/home_selected"
    android:drawablePadding="@dimen/drawer_drawable_padding"
    android:drawableStart="@mipmap/home_selected"
    android:gravity="center_vertical"
    android:padding="@dimen/padding_field"
    android:text="@string/home"
    android:textColor="@android:color/white" />

使用 drawableLeft 它会自动将您的 TextView 与图像对齐

在适配器的布局中,只需将此 xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:margin="5dp"
    android:padding="5dp"
    android:gravity="center"
    android:orientation="horizontal">
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding_left="5dp"
    android:padding_right="5dp"
    android:src="@drawable/ic_launcher"/>
<TextView
    android:id="@+id/home"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="@string/home"
    android:textColor="@android:color/white" />
</LinearLayout>

你可以这样做

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:margin="5dp"
    android:padding="5dp"
    android:gravity="center"
    android:orientation="horizontal">
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding_left="5dp"
    android:padding_right="5dp"
    android:src="@drawable/ic_launcher"/>
<TextView
    android:id="@+id/home"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:text="@string/home"
android:gravity="center"
    android:textColor="@android:color/white" />
</LinearLayout>

您可以在 android Studio 中为 导航抽屉(边栏)使用预定义 Activity...

仅添加 android:layout_marginTop 即可正常工作。

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="48dp" 
        android:background="@drawable/list_selector">

        <ImageView
            android:id="@+id/icon"
            android:layout_width="25dp"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="12dp"
            android:contentDescription="@string/desc_list_item_icon"
            android:src="@drawable/ic_home"
            android:layout_centerVertical="true" />

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_toRightOf="@id/icon"
            android:minHeight="?android:attr/listPreferredItemHeightSmall"
            android:textAppearance="?android:attr/textAppearanceListItemSmall"
            android:textColor="@color/list_item_title"
            android:gravity="center_vertical"
            android:layout_marginTop="16dp"
            android:paddingRight="40dp"/>

        <TextView android:id="@+id/counter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/counter_bg"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="8dp"
            android:textColor="@color/counter_text_color"/>

    </RelativeLayout>