2016-07-05 21 views
3

enter image description here我想以編程方式在Android工具欄中更改導航圖標的高度和寬度(以屏幕截圖中的黑圈表示)。有沒有辦法做到這一點。這不是工具欄徽標。我無法更新Styles xml中的工具欄主題,因爲我希望它是動態的。請幫忙。Android工具欄 - 以編程方式更改導航圖標的高度和寬度

+0

你試過改變它嗎?發佈您的代碼,以便我們可以幫助您更多 –

+0

它通過xml更改,但我無法動態更新xml。我希望它動態地完成。 –

+0

@anuja_k你想改變的圖標,你可以提供任何截圖,然後我會幫你嗎? –

回答

4

我沒有這樣說:

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

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> 

如果您要訪問我的Toolbar

任何視圖我n工具欄請參閱@AleksandarStefanović的回答。你可以在Material design guidelines看一看用於創建自定義Toolbar

然後截圖:

enter image description here

圖標來自:Material Icons

+0

只是通過縮放位圖它完美地工作。謝謝! –

+0

@anuja_k很好,我發佈了兩種方式,也許任何人都會在將來使用它 –

1

如果您使用的是程序兼容性工具欄,它實際上表現爲一個常規ViewGroup。因此,您可以輕鬆訪問其中的Views,就像使用其他ViewGroup一樣。

例如,如果你在你裏面工具欄有一個ImageView的,你可以簡單地訪問它:

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

從那裏,你只要調整它作爲一個經常ImageView的。

+0

是的,我正在使用AppCompat工具欄。是否有任何預先聲明的導航圖標ID? –

相關問題