[英]AppCompat v21 Toolbar changing logo size
我正在從上一個操作欄遷移到appcompat v21中的新工具欄功能。 我仍然希望將徽標保留在操作欄(工具欄)的左上角部分。 為了做到這一點,我在我的布局中添加了支持工具欄,我為它創建了一個新的。
app:theme="@style/NewToolBarStyle"
我正在以編程方式添加日志,因為應用程序中有一些邏輯。
actionBar.setLogo(R.drawable.myicon);
參考我的新風格(暫時為空):
<style name="NewToolBarStyle" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
</style>
然而,結果是顯示的圖像對於我正在尋找的東西來說太大了,我想知道如何減小圖標的大小。
有什么方法(風格,布局或編程)我可以減少徽標大小?
材料設計中沒有徽標圖標: http : //www.google.com/design/spec/layout/structure.html# ,所以我認為這不是經過良好測試的場景 - 或者設計簡單(破損)。 您可以將ImageView添加為工具欄的子窗口小部件,並使用它來顯示任何圖像。 它將顯示在所有其他內部窗口小部件的右側 - 如微調器 - 但列表導航模式也已棄用。
如果您堅持使用徽標,那么我的解決方法是確保工具欄具有固定高度 - 這會處理錯誤的圖標高度。 即使在此之后,您還必須在工具欄內部徽標ImageView上將setAdjustViewBounds設置為true - 否則它將創建大的左右填充。
這就是我的工具欄的樣子(高度設置為?attr / actionBarSize):
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
使用以下內容在活動布局中引用它:
<include layout="@layout/toolbar_actionbar"/>
不要在include中更改layout_height。
第二步是在徽標圖標上設置AdjustViewBounds(true):
Drawable logo = getDrawable(iconRes);
toolbar.setLogo(logo);
for (int i = 0; i < toolbar.getChildCount(); i++) {
View child = toolbar.getChildAt(i);
if (child != null)
if (child.getClass() == ImageView.class) {
ImageView iv2 = (ImageView) child;
if ( iv2.getDrawable() == logo ) {
iv2.setAdjustViewBounds(true);
}
}
}
根據@brightstar提出的建議,我想進一步發展答案。
在新工具欄中控制徽標大小和位置的最佳方法是實際上不使用它。 這個概念完全不同,您需要做的是從頭開始創建一個工具欄。 因此,您需要做出決定,要么使用actionBar給出的布局,要么包含所有新內容,包括標題。
如果您停止使用徽標,但繼續使用標題,您最終會看到徽標已超出標題創建和ood情況。
因此,如何做的一個例子如下:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/action_bar_background"
app:theme="@style/NewToolBarStyle"
android:minHeight="?attr/actionBarSize" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/text_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="17dp"
android:textSize="20sp"
android:layout_toRightOf="@+id/logo_image"
android:text="@string/app_name"
android:textColor="@color/white" />
<ImageView
android:id="@+id/logo_image"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:scaleType="centerInside" />
</RelativeLayout>
</FrameLayout>
您將此文件創建為my_toolbar.xml。 請注意以下細節:
稍后在@brightstar的描述中,您需要通過include包含在布局的頂部,但是....記得添加一個id,以便您可以將所有其他視圖引用到它。
<include layout="@layout/toolbar_sharemup"
android:id="@+id/including" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.