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

enter image description here

我想以编程方式更改Android工具栏中导航图标(屏幕截图中的黑色圆圈)的高度和宽度.有没有办法这样做.这不是工具栏徽标.我无法更新Styles xml中的工具栏主题,因为我希望它是动态的.请帮忙.

解决方法:

我是这样做的:

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);

}

创建缩放位图时,可以使用calculate-dp-from-pixels-in-android-programmaticallyconverting-pixels-to-dp.

我的工具栏:

<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" />

截图:

enter image description here

另一种方法是在工具栏中使用自定义布局,如下所示:

<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>

如果您想访问工具栏中的任何视图,请参阅@AleksandarStefanović的回答.您可以查看Material design guidelines以创建自定义工具栏

然后截图:

enter image description here

图标来自:Material Icons

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...