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