2017-04-07 128 views
2

在我的項目中,我正在擴展android.support.v7.widget.Toolbar以向班級添加一些額外的功能。但是,當我在佈局文件中實現此類時,它會更改工具欄上顯示的圖標的邊距(或填充,不確定...)。從AppCompat繼承工具欄更改工具欄圖標邊距

默認android.support.v7.widget.Toolbar結果:

android.support.v7.widget.Toolbar

自定義工具欄類結果:

Custom Toolbar

我的自定義工具欄類已經沒有多餘的代碼,它只是實現所需的構造函數,所以我不操縱邊界我的自。

這裏的自定義工具欄類:

import android.content.Context; 
import android.support.annotation.Nullable; 
import android.support.v7.widget.Toolbar; 
import android.util.AttributeSet; 


public class ThemedToolbar extends Toolbar { 

    public ThemedToolbar(Context context) { 
     this(context, null); 
    } 

    public ThemedToolbar(Context context, @Nullable AttributeSet attrs) { 
     this(context, attrs, 0); 
    } 

    public ThemedToolbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 
} 

而這裏的我,包括我的所有活動的工具欄佈局文件:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="?actionBarSize"> 

    <com.endare.ui.theme.view.ThemedToolbar 
     android:id="@+id/toolbar_control" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@android:color/white" /> 

    <RelativeLayout 
     android:id="@+id/toolbar_content_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal"> 

     <ImageView 
      android:id="@+id/toolbar_logo" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_centerInParent="true" 
      android:layout_marginBottom="5dp" 
      android:layout_marginTop="5dp"/> 

    </RelativeLayout> 

</FrameLayout> 

所以基本上,我所做的佈局文件看到不同的結果是切換<android.support.v7.widget.Toolbar<com.endare.ui.theme.view.ThemedToolbar

如何防止改變圖標邊距的自定義工具欄實現?

回答

1

您必須在第二個構造函數中將R.attr.toolbarStyle作爲defStyleAttr參數。