微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

原文地址:https://www.jb51.cc/android/1071436.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐