【问题标题】:Get ImageView from getSupportActionBar()从 getSupportActionBar() 获取 ImageView
【发布时间】:2015-01-06 08:41:45
【问题描述】:

我正在使用getSupportActionBar().setIcon(R.drawable.ic_transparent); 来设置工具栏的图标。如何从该图标获得ImageView?我需要图标的Rect

我正在尝试用Toolbar 代替旧的操作栏来实现this 的版本。

编辑:我想出了一个替代解决方案,将 TextView 和 ImageView 放在工具栏中,而不是使用内置的徽标和标题。这在 Android 5.0 上运行良好,但文本不会显示在低于此的任何内容上。有什么建议可以解决这个问题或上述问题?

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/actionBarSize"
    android:background="@android:color/transparent"
    android:elevation="10dp"
    tools:ignore="UnusedAttribute">

    <ImageView
        android:id="@+id/toolbar_icon"
        android:layout_width="?android:attr/actionBarSize"
        android:layout_height="?android:attr/actionBarSize"
        android:padding="10dp"
        android:scaleType="fitStart"
        android:src="@drawable/ic_transparent" />

    <TextView
        android:id="@+id/toolbar_title"
        style="@style/Base.TextAppearance.AppCompat.Widget.ActionBar.Title"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:text="hello" />

</android.support.v7.widget.Toolbar>

【问题讨论】:

    标签: android toolbar material-design


    【解决方案1】:

    很难说。

    正确答案:你不应该弄乱不是你自己设计的内部小部件(即,如果没有针对 ImageView 的 getter 方法,你应该把它留在原处)。见here

    更老套的答案:您可能想试试this custom method。它根据他们的ContentDescription 搜索视图。我从未尝试过,但它可以工作。

    【讨论】:

    • 从哪里获得徽标的内容描述?这是与导航图标 AFAIK 不同的元素。
    • 我认为它应该也可以。如果没有,请使用 toolbar.setNavigationIcon() 而不是 actionbar.setIcon()。
    • 我不想使用导航图标,因为它是可点击的。不管怎样,我现在解决了。
    【解决方案2】:

    我看了一下,刚刚看到一个私有函数 getActionBarIconView(),它在活动 NoBoringActionBarActivity 中返回一个图像视图,如果您需要在外部片段中使用它,只需将其更改为公共。

    【讨论】:

      【解决方案3】:

      我通过将自己的ImageView和TextView添加到工具栏来替换logo和标题来解决它,然后通过将工具栏移动到布局文件的末尾来修复android 4.0-4.4上的不可见标题。我不知道为什么我必须这样做,但它确实有效。

      【讨论】:

        猜你喜欢
        • 2012-02-21
        • 2023-03-21
        • 1970-01-01
        • 1970-01-01
        • 2019-01-07
        • 2013-02-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多