AppCompat v21工具栏如何更改Logo大小

8

我正在从之前的操作栏迁移到appcompat v21中的新工具栏功能。我仍然希望将标志放在操作栏(工具栏)左上角。为此,我在我的布局中添加了支持工具栏,并为其创建了一个新主题。

    app:theme="@style/NewToolBarStyle"

由于应用程序中存在某些逻辑,我正在以编程方式添加日志。

            actionBar.setLogo(R.drawable.myicon);

提到我的新样式(目前为空):

<style name="NewToolBarStyle" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
</style>

然而,结果显示的图像对我来说太大了,我想知道如何缩小图标的尺寸。是否有任何方法(样式、布局或编程),可以缩小徽标的尺寸?请保留HTML标记。
2个回答

8
在Material Design中没有标志图标:http://www.google.com/design/spec/layout/structure.html#,因此我认为这不是经过充分测试的场景 - 或者仅仅是设计上的问题。您可以将ImageView添加为工具栏的子部件,并使用它来显示任何图像。它将显示在所有其他内部小部件的右侧 - 如微调控制器 - 但列表导航模式也已弃用。
如果您坚持要有标志,则我的解决方法是确保工具栏具有固定高度 - 这会处理错误的图标高度。即使在那之后,您还必须将setAdjustViewBounds设置为true,以在工具栏的内部标志ImageView上进行设置 - 否则它将创建大量的左右填充。
这就是我的工具栏的外观(高度设置为?attr/actionBarSize):
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_height="?attr/actionBarSize"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>

使用以下方式在您的Activity布局中引用它:

<include layout="@layout/toolbar_actionbar"/>

不要在include中更改layout_height。

第二步是在logo图标上设置setAdjustViewBounds(true):

    Drawable logo = getDrawable(iconRes);
    toolbar.setLogo(logo);
    for (int i = 0; i < toolbar.getChildCount(); i++) {
      View child = toolbar.getChildAt(i);
      if (child != null)
        if (child.getClass() == ImageView.class) {
          ImageView iv2 = (ImageView) child;
          if ( iv2.getDrawable() == logo ) {
            iv2.setAdjustViewBounds(true);
          }
        }
    }

谢谢你的建议。我想做的只是修改标志的大小,保持操作栏不变(使标志相对于操作栏更小)。所以按照你的想法,我拿了iv2(我的标志图像视图),试图修改标志的参数,但它崩溃了。你知道我能不能这样做吗? - Trebia Project.
@TrebiaProject。如果您遇到崩溃,请查看logcat/exceptions,但这种方式太过粗糙了。为什么不将ImageView作为工具栏的子小部件添加呢? - marcinj
好的...我不知道这样的事情是可能的。但这将意味着将标题更改为文本视图。关于主页按钮,你知道如何获取它的大小,以便我引入的图像不会重叠吗? - Trebia Project.
谢谢,对于我来说图标太大了,而通过您的方法我可以更改填充: int padding =(int)(5 * getResources()。getDisplayMetrics()。density); iv2.setPadding(padding,padding,padding,padding); - Toni Alvarez
谢谢,这真的很有帮助,解决了在使用非正方形图像时出现错误的标志布局问题。别忘了在找到视图后添加break以避免不必要的循环;) - Norman

6

在@brightstar的建议下,我希望进一步发展答案。

控制新工具栏中Logo的大小和位置的最佳方法实际上是不使用它。概念完全不同,您需要从头创建工具栏。因此,您需要做出决定,要么使用actionBar提供的布局,要么包括所有新内容,包括标题。

如果停止使用标志但继续使用标题,则最终将看到标志位于标题之上,从而创建一个糟糕的情况。

因此,要执行的示例如下:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/action_bar_background"
        app:theme="@style/NewToolBarStyle"
        android:minHeight="?attr/actionBarSize" />
<RelativeLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/text_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="17dp"
        android:textSize="20sp"
        android:layout_toRightOf="@+id/logo_image"
        android:text="@string/app_name"
        android:textColor="@color/white" />

    <ImageView
        android:id="@+id/logo_image"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:scaleType="centerInside" />

</RelativeLayout>
</FrameLayout>

您需要创建一个名为my_toolbar.xml的文件,注意以下细节:
  • 我没有包括ImageView的src,因为我会在运行时动态更改它。但是如果添加它,则可以工作。
  • 我使用了相对布局,以便能够将图标和文本居中。
  • 我仍然需要包括Home按钮。
稍后,根据@brightstar的描述,您需要通过include将其包含在布局顶部,但是请记得添加一个ID,以便您可以将所有其他视图引用到它。
 <include layout="@layout/toolbar_sharemup"
    android:id="@+id/including" />

你的外层FrameLayout的目的是什么?在你的布局中,工具栏本身就足够了。此外,在include中的id只有在你想要覆盖所包含项的id时才需要。它真正必要的唯一时间是在使用相对布局并且你想要相对于所包含的项定位其他项时;如果是这样,请确保include id和所包含的项的id匹配。 - Lorne Laliberte
我的include有一个id,这样我就可以相对定位其他项目。我包含了FrameLayout,因为我无法在同一布局中同时包含相对布局和工具栏。你有其他建议吗? - Trebia Project.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接