我想以编程方式更改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-programmatically和converting-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" />
截图:
<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以创建自定义工具栏
然后截图:
图标来自:Material Icons