Android工具栏-以编程方式更改导航图标的高度和宽度 [英] Android Toolbar - Change height and width of Navigation Icon Programmatically
问题描述
我想以编程方式更改Android工具栏中的导航图标"的高度和宽度(在屏幕截图中以黑色圆圈显示).有没有办法做到这一点.这不是工具栏徽标.我无法更新Styles xml中的工具栏主题,因为我希望它是动态的.请帮忙.
I want to change height and width of Navigation Icon(in black circle in screen shot) in Android Toolbar programmatically. 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. Please help.
推荐答案
我是这样做的:
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-创建缩放后,在Android中以编程方式 和 converting-pixels-to-dp 位图.
我的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" />
和屏幕截图:
另一种方法是像这样在Toolbar
中使用自定义布局:
Also another way is using custom layout in Toolbar
like that:
<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ć的答案.您可以查看材料设计指南,以创建自定义
If you want to access any View in Toolbar see @Aleksandar Stefanović's answer. And you can take a look at Material design guidelines for creating custom Toolbar
然后截图:
来自以下图标的图标:材质图标
Icons from : Material Icons
这篇关于Android工具栏-以编程方式更改导航图标的高度和宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!