AppCompat v21工具栏更改徽标大小

Tre*_*ct. 7 android android-appcompat android-5.0-lollipop

我正在从上一个操作栏迁移到appcompat v21中的新工具栏功能.我仍然希望将徽标保留在操作栏(工具栏)的左上角部分.为了做到这一点,我在我的布局中添加了支持工具栏,我为它创建了一个新的.

    app:theme="@style/NewToolBarStyle"
Run Code Online (Sandbox Code Playgroud)

我正在以编程方式添加日志,因为应用程序中有一些逻辑.

            actionBar.setLogo(R.drawable.myicon);
Run Code Online (Sandbox Code Playgroud)

参考我的新风格(暂时为空):

<style name="NewToolBarStyle" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
</style>
Run Code Online (Sandbox Code Playgroud)

然而,结果是显示的图像对于我正在寻找的东西来说太大了,我想知道如何减小图标的大小.

有什么方法(风格,布局或编程)我可以减少徽标大小?

mar*_*inj 8

材料设计中没有徽标图标:http://www.google.com/design/spec/layout/structure.html#,所以我认为这不是经过良好测试的场景 - 或者设计简单(破损).您可以将ImageView添加为工具栏的子窗口小部件,并使用它来显示任何图像.它将显示在所有其他内部窗口小部件的右侧 - 如微调器 - 但列表导航模式也已弃用.

如果您坚持使用徽标,那么我的解决方法是确保工具栏具有固定高度 - 这会处理错误的图标高度.即使在此之后,您还必须在工具栏内部徽标ImageView上将setAdjustViewBounds设置为true - 否则它将创建大的左右填充.

这就是我的工具栏的样子(高度设置为?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>
Run Code Online (Sandbox Code Playgroud)

使用以下内容在活动布局中引用它:

<include layout="@layout/toolbar_actionbar"/>
Run Code Online (Sandbox Code Playgroud)

不要在include中更改layout_height.

第二步是在徽标图标上设置AdjustViewBounds(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);
          }
        }
    }
Run Code Online (Sandbox Code Playgroud)


Tre*_*ct. 6

根据@brightstar提出的建议,我想进一步发展答案.

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

如果您停止使用徽标,但继续使用标题,您最终会看到徽标已超出标题创建和ood情况.

因此,如何做的一个例子如下:

<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>
Run Code Online (Sandbox Code Playgroud)

您将此文件创建为my_toolbar.xml.请注意以下细节:

  • 我没有包含我的ImageView的src,因为我正在改变它的dinamically.但是添加它的工作.
  • 我使用相对布局来使图标和文本居中.
  • 我还需要包含Home按钮.

稍后在@brightstar的描述中,您需要通过include包含在布局的顶部,但是....记得添加一个id,以便您可以将所有其他视图引用到它.

 <include layout="@layout/toolbar_sharemup"
    android:id="@+id/including" />
Run Code Online (Sandbox Code Playgroud)