简体   繁体   English

Android工具栏 - 以编程方式更改导航图标的高度和宽度

[英]Android Toolbar - Change height and width of Navigation Icon Programmatically

在此输入图像描述 I want to change height and width of Navigation Icon(in black circle in screen shot) in Android Toolbar programmatically. 我想以编程方式在Android工具栏中更改导航图标的高度和宽度(在屏幕截图中的黑色圆圈中)。 Is there any way to do so. 有没有办法这样做。 This is not toolbar logo. 这不是工具栏徽标。 I can't update toolbar theme in Styles xml as I want it to be dynamic. 我无法更新Styles xml中的工具栏主题,因为我希望它是动态的。 Please help. 请帮忙。

I did this way : 我是这样做的:

toolbar= (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if(getSupportActionBar()!=null){
    Drawable drawable= getResources().getDrawable(R.drawable.ic_sync_white_36dp);
    Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
    Drawable newdrawable = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, 250, 250, true));
    newdrawable.setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_ATOP);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeAsUpIndicator(newdrawable);

}

You can use calculate-dp-from-pixels-in-android-programmatically and converting-pixels-to-dp when creating scaled bitmap . 您可以在编程时使用calculate-dp-from-pixels-in-android-编程并在创建缩放位图时将像素转换为dp

My Toolbar : 我的Toolbar

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

And screenshot : 截图:

在此输入图像描述

Also another way is using custom layout in Toolbar like that: 另一种方法是在Toolbar使用自定义布局,如下所示:

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" >
    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal" android:layout_width="match_parent"
        android:background="@color/colorPrimary"
        android:id="@+id/custom_toolbar_layout"
        android:layout_height="wrap_content">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:tint="@android:color/holo_purple"
        android:id="@+id/imageView"
        android:gravity="center_vertical"
        android:src="@drawable/ic_clear_black_36dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="My Custom Toolbar"
        android:gravity="center_vertical"
        android:textSize="20sp"
        android:textColor="@android:color/white"
        android:layout_gravity="center_horizontal" />
    </LinearLayout>
</android.support.v7.widget.Toolbar>

If you want to access any View in Toolbar see @Aleksandar Stefanović's answer. 如果您想访问工具栏中的任何视图,请参阅@AleksandarStefanović的回答。 And you can take a look at Material design guidelines for creating custom Toolbar 您可以查看用于创建自定义Toolbar 材料设计指南

Then screenshot : 然后截图:

在此输入图像描述

Icons from : Material Icons 图标来自: 材料图标

If you're using an appcompat Toolbar, it actually behaves like a regular ViewGroup. 如果您使用的是appcompat工具栏,它实际上就像常规ViewGroup一样。 So, you can access the Views inside of it, easily, like with any other ViewGroup. 因此,您可以轻松地访问其中的视图,就像使用任何其他ViewGroup一样。

For example, if you have an ImageView inside you toolbar, you can simply access it by: 例如,如果工具栏中有ImageView,则可以通过以下方式访问它:

        getSupportActionBar().getCustomView().findViewById(R.id.imageView)

From there, you just resize it as a regular ImageView. 从那里,您只需将其调整为常规ImageView。

try this in AppCompatActivity 在AppCompatActivity中尝试这个

if(getSupportActionBar()!=null){
  getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  getSupportActionBar().setHomeButtonEnabled(true);
}
toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {
    getSupportFragmentManager().popBackStack();
  }});

in xml 在xml中

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="90dp"
        android:background="#434343"
        android:elevation="4dp"
        android:minHeight="90dp"
        app:theme="@style/tool_bar_style" />

in styles.xml 在styles.xml中

    <style name="tool_bar_style">
    <item name="colorControlNormal">@color/white</item>
    <item name="android:textColor">@color/text_color_main</item>
    <item name="android:textColorPrimary">@color/text_color_main</item>
    <item name="android:textColorSecondary">@color/text_color_main</item>
    <item name="actionMenuTextColor">@color/text_color_main</item>
    <item name="android:textSize">@dimen/text_size_default</item>
    <item name="android:colorBackground">@color/toolbar_bg_color</item>
    <item name="android:listPreferredItemHeightSmall">@dimen/button_default_height</item>

</style>
<style name="Toolbar.TitleText" parent="TextAppearance.AppCompat.Widget.ActionBar.Title">">
    <item name="android:textSize">@dimen/text_size_big</item>
</style>

in dimens.xml 在dimens.xml中

    <dimen name="text_size_big">14sp</dimen>
    <dimen name="text_size_default">22sp</dimen>
    <dimen name="button_default_height">90dp</dimen>

in colors 在颜色

    <color name="text_color_main">@color/clean_white</color>
    <color name="toolbar_bg_color">#434343</color>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android中以编程方式更改位图的高度和宽度? - How to change bitmap height and width programmatically in android? 如何以编程方式更改android工具栏中的图标? - How can I change the icon in an android toolbar programmatically? 将图标更改为Android工具栏 - Change the icon to Toolbar Android 如何以编程方式获取 Android 导航栏的高度和宽度? - How do I get the height and width of the Android Navigation Bar programmatically? 以编程方式设置工具栏高度Android - set Toolbar height programmatically Android 返回主屏幕时,Android工具栏导航图标不会变回“ 3行”图标 - Android Toolbar Navigation icon doesnt change back to “3 lines” icon when returning to main screen 以编程方式更改工具栏中的菜单图标颜色 - Programmatically change menu icon color in Toolbar 在工具栏中更改ActionBarDrawerToggle图标android? - Change ActionBarDrawerToggle icon android in toolbar? 如何在Android工具栏导航图标中设置复选框? - How to set an checkbox in the Android toolbar navigation icon? Android:在工具栏中增加“导航”图标的触摸区域 - Android: Increase the touch area of Navigation icon in the toolbar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM