【问题标题】:Navigation menu item "badge" vertically alignment导航菜单项“徽章”垂直对齐
【发布时间】:2016-10-12 20:44:31
【问题描述】:

您好,我在导航菜单中使用徽章计数器,但是。我不能移动到垂直居中。我尝试了边距,高度:匹配父级,但宽度形状不成功。

(https://stackoverflow.com/a/33607630/4933464) 这是我的代码(我使用形状):

@drawable/menu_counter.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#ec443e" />
    <padding
        android:left="5dp"
        android:right="5dp"
        android:top="3dp"
        android:bottom="3dp"/>
    <corners android:radius="5dp" />
</shape>

@layout/menu_counter.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@drawable/menu_counter"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
android:textColor="#ffffff" />

谢谢。

【问题讨论】:

  • 任何更新我有同样的问题?

标签: java android android-layout android-studio


【解决方案1】:

原始 DrawerLayout 不支持徽章。但是有很多有用的库

MaterialDrawerhttps://github.com/mikepenz/MaterialDrawer

github页面上的截图。

【讨论】:

    【解决方案2】:

    用相对布局包裹它并添加

    android:layout_centerVertical="true"

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="wrap_content"
          android:layout_height="match_parent">
    
          <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:gravity="center"
           android:layout_centerVertical="true"
           android:background="@drawable/menu_counter"
           android:textAppearance="@style/TextAppearance.AppCompat.Body2"
           android:textColor="#ffffff" />
    </RelativeLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-27
      • 2017-06-21
      • 2019-04-08
      • 2016-01-18
      • 2021-05-30
      相关资源
      最近更新 更多