AppCompat v21工具栏更改徽标大小 [英] AppCompat v21 Toolbar changing logo size

查看:108
本文介绍了AppCompat v21工具栏更改徽标大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从上一个操作栏迁移到appcompat v21中的新工具栏功能.我仍然希望将徽标保留在操作栏(工具栏)的左上方.为此,我在布局中添加了支持工具栏,并为此创建了一个新的样式.

I am migrating to the new Toolbar feature in appcompat v21 from the previous action bar. I still want to keep the logo on the top left part of the actionbar (toolbar). For doing I added in my layout the support toolbar and I created a new thene for it.

    app:theme="@style/NewToolBarStyle"

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

I am adding the log programmatically as there is some logic in the app for this.

            actionBar.setLogo(R.drawable.myicon);

请参阅我的新样式(目前为空):

Referring to my new style (empty for the moment):

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

但是结果显示的图像对于我要寻找的图像来说太大了,我想知道如何减小图标的大小.

However the result is showing an image the is too big for what I am looking for, and I am wondering how to reduce the size of the icon.

是否可以通过任何方式(样式,布局或编程)减小徽标大小?

Is there any way (style, layout or programming) that I can reduce the logo size?

推荐答案

在@brightstar给出的建议之后,我想进一步阐述答案.

Following the suggestiong give by @brightstar I would like to develop further the answer.

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

The best way to control the size and position of the Logo in the new Toolbar is by actually not using it. The concept is completely different, what you need to do is create a toolbar from scratch. So you need to make a decision, either you use the layout given by the actionBar or include everything new including the title.

如果您停止使用徽标,但继续使用标题,则最终您会看到徽标超出了标题的创建和覆盖范围.

If you stop using the logo but you keep using the title you finally will see that the logo is over the title creating and ood situation.

因此,下面是一个示例:

So, an example of what to do is the following:

<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.请注意以下详细信息:

You create this file as my_toolbar.xml. Notice the following details:

  • 我没有包括ImageView的src,因为我正在对其进行动态更改.但是可以添加它.
  • 我使用了相对的布局,以便能够使图标和文本居中.
  • 我仍然需要包含主页"按钮.

稍后,按照@brightstar的说明,您需要在包含布局的顶部添加一个include,但是....请记住添加一个id,以便您可以引用其所有其他视图.

Later on as described by @brightstar you need to include in the top of your layouts by an include, however.... remember to add an id so that you can refere all your other Views to it.

 <include layout="@layout/toolbar_sharemup"
    android:id="@+id/including" />

这篇关于AppCompat v21工具栏更改徽标大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆