2017-04-07 11 views
2

私のプロジェクトでは、android.support.v7.widget.Toolbarを拡張してクラスにいくつかの機能を追加しています。しかし、レイアウトファイルにこのクラスを実装すると、ツールバーに表示されるアイコンのマージン(またはパディング、わかりません...)が変更されます。AppCompatツールバーからの継承ツールバーのアイコンの余白の変更

デフォルトandroid.support.v7.widget.Toolbar結果:

android.support.v7.widget.Toolbar

カスタムツールバーのクラス結果:

Custom Toolbar

マイカスタムツールバークラスは余分を持っていませんコードはまだ必要なコンストラクタを実装するだけなので、私はマージンを操作していません自己。

ここでは、カスタムツールバーのクラスです:

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

だから、基本的には、レイアウトファイルに私がしたすべてに:

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); 
    } 
} 

そして、ここでは、私はすべての私の活動に含めていますツールバーのレイアウトファイルです異なる結果を参照してください<android.support.v7.widget.Toolbar<com.endare.ui.theme.view.ThemedToolbarと切り替えることです。

アイコンの余白を変更するカスタムツールバーの実装を防止するにはどうすればよいですか?

答えて

1

defStyleAttrパラメータとしてR.attr.toolbarStyleを2番目のコンストラクタに指定する必要があります。

関連する問題