在我的项目中,我正在扩展android.support.v7.widget.Toolbar
以向该类添加一些额外的功能。但是,当我在布局文件中实现此类时,它会更改工具栏上显示的图标的边距(或填充,不确定...)。
默认android.support.v7.widget.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
切换。
如何阻止我的自定义工具栏实现更改图标的边距?
答案 0 :(得分:1)
您必须将R.attr.toolbarStyle
作为defStyleAttr
参数放在第二个构造函数中。