我想以編程方式在Android工具欄中更改導航圖標的高度和寬度(以屏幕截圖中的黑圈表示)。有沒有辦法做到這一點。這不是工具欄徽標。我無法更新Styles xml中的工具欄主題,因爲我希望它是動態的。請幫忙。Android工具欄 - 以編程方式更改導航圖標的高度和寬度
回答
我沒有這樣說:
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" />
和屏幕截圖:
在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
然後截圖:
圖標來自:Material Icons
只是通過縮放位圖它完美地工作。謝謝! –
@anuja_k很好,我發佈了兩種方式,也許任何人都會在將來使用它 –
如果您使用的是程序兼容性工具欄,它實際上表現爲一個常規ViewGroup。因此,您可以輕鬆訪問其中的Views,就像使用其他ViewGroup一樣。
例如,如果你在你裏面工具欄有一個ImageView的,你可以簡單地訪問它:
getSupportActionBar().getCustomView().findViewById(R.id.imageView)
從那裏,你只要調整它作爲一個經常ImageView的。
是的,我正在使用AppCompat工具欄。是否有任何預先聲明的導航圖標ID? –
- 1. 以編程方式在ViewDidLoad()中不更改導航欄高度?
- 2. 以編程方式更改Textview的高度和寬度
- 3. 以編程方式更改UIImageView Xcode的高度和寬度Swift
- 4. 導航欄更改高度
- 5. 更改導航欄高度
- 6. 以編程方式更改ColorBox的寬度/高度onClick
- 7. 如何以編程方式更改Swift中導航欄的高度?
- 8. 以編程方式獲取導航欄的高度
- 9. 以編程方式更改小部件寬度/高度的最佳方法?
- 10. Android以編程方式更改視圖的寬度
- 11. 更改摺疊導航欄的寬度
- 12. 如何以編程方式更改框架佈局高度和寬度?
- 13. Android:以編程方式更改佈局根視圖的高度
- 14. 如何更改導航欄的高度?
- 15. TimelineJS:更改導航欄的高度?
- 16. 更改側欄導航的高度
- 17. UITableView,如何以編程方式更改整個tableview的寬度和高度?
- 18. 更改工具欄和CollapsingToolbarLayout以編程方式滾動標誌
- 19. 如何更改導航欄高度
- 20. 以編程方式設置寬度/視圖高度
- 21. 更改圖像的寬度和高度
- 22. 更改圖像的高度和寬度
- 23. 以編程方式創建ImageView的寬度和高度
- 24. 以編程方式設置GestureOverlayView的寬度和高度?
- 25. 以編程方式獲取佈局的高度和寬度
- 26. 以編程方式更改標籤欄和導航欄的顏色
- 27. 以編程方式更改導航欄標題
- 28. 以編程方式更改導航欄標題
- 29. 以編程方式更改android中按鈕的寬度
- 30. Android - 如何以編程方式更改ImageButton的寬度?
你試過改變它嗎?發佈您的代碼,以便我們可以幫助您更多 –
它通過xml更改,但我無法動態更新xml。我希望它動態地完成。 –
@anuja_k你想改變的圖標,你可以提供任何截圖,然後我會幫你嗎? –