簡體   English   中英

Android工具欄 - 以編程方式更改導航圖標的高度和寬度

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

在此輸入圖像描述 我想以編程方式在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-編程並在創建縮放位圖時將像素轉換為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使用自定義布局,如下所示:

<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ć的回答。 您可以查看用於創建自定義Toolbar 材料設計指南

然后截圖:

在此輸入圖像描述

圖標來自: 材料圖標

如果您使用的是appcompat工具欄,它實際上就像常規ViewGroup一樣。 因此,您可以輕松地訪問其中的視圖,就像使用任何其他ViewGroup一樣。

例如,如果工具欄中有ImageView,則可以通過以下方式訪問它:

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

從那里,您只需將其調整為常規ImageView。

在AppCompatActivity中嘗試這個

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

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

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

在dimens.xml中

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

在顏色

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM