gpt4 book ai didi

android - MenuItem actionView 右对齐

转载 作者:太空狗 更新时间:2023-10-29 13:24:34 26 4
gpt4 key购买 nike

正如标题所说,我在 MenuItem 上使用 setActionView 进行设置,布局如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ProgressBar
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_margin="4dp"
android:layout_centerInParent="true"/>

</RelativeLayout>

问题是 ProgressBar 显示为右对齐。 (它应该与中心对齐,与 MenuItem 图标相同。图标为 32dp

我怎样才能做到这一点?

最佳答案

我今天遇到了类似的问题,但在 SO 上找不到任何好的 QA。因此我做了一些研究。以下是我对此事的调查结果:

Android Menu is implemented in this way:

如果您将 actionView 膨胀为:

  • 图标(可选)
  • 标题(可选,或者说可以通过将其设置为“”)使其成为可选的
  • 自定义 View

并且您不设置图标,将标题设置为“”并设置 actionView 以使 menuItem 看起来仅包含您使用 setActionView 扩充的自定义 View ,然后:

enter image description here

Android forces the custom view's width to be wrap_content and remaining area gets occupied by icon+title of the menuItem.

我们可以将此实现视为:

enter image description here

enter image description here

将 margin 之后的整个 View 想象成 ParentLinearLayout,它分为 LinearLayout1LinearLayout2(上图中描述为 Layout 1 和布局2分别)

现在 LinearLayout1width=0dpweight=1LinearLayout2width=wrap_content

因此无论我们设置什么,目前我们都无法在菜单中右对齐膨胀的自定义 View (action view)。

To be technically accurate, you can right align if you really need this done by checking out the internal implementation code for the navigation menu by Android. But not really worth the effort. Any feature is a tradeoff of time required vs outcome.

关于android - MenuItem actionView 右对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22866378/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com