自定义图像作为操作栏徽标边距?
Custom image as action bar logo margin?
我正在尝试添加一个自定义图像作为操作栏徽标,这可行,但是,当它被添加到操作栏时,此图像前面有大约 30-50dp 的填充。
图像本身没有左侧填充,因此它应该几乎紧挨着屏幕边缘。
这个填充在运行时是从哪里添加的?这是我用于操作栏的自定义布局的代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/actionbar">
<ImageView
android:id="@+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/logo"
</RelativeLayout>
这是我在 activity:
中设置操作栏布局的代码
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
LayoutInflater inflator = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.actionbar, null);
actionBar.setCustomView(v);
我发现删除此填充的唯一方法是将 android:layout_marginLeft="-30dp"
添加到自定义操作栏的布局中,但我绝对不认为这是正确的方法。
知道为什么我的图片会有额外的填充吗?在此先感谢您的帮助!
编辑
我设法解决了这个问题,我在 activity 的操作栏设置中添加了以下两行:
Toolbar parent = (Toolbar) v.getParent();
parent.setContentInsetsAbsolute(0, 0);
尝试修改 styles.xml
中的 ActionBar 样式
<style name="MyActionBarTheme" parent="Widget.AppCompat.ActionBar">
<item name="contentInsetStart">0dp</item>
<item name="contentInsetEnd">0dp</item>
</style>
然后就是你的App Theme
<item name="android:actionBarStyle">@style/MyActionBarTheme</item>
或 如果您正在使用 ToolbarLayout
<android.support.v7.widget.Toolbar
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:paddingLeft="0dp">
<!--rest of content -->
</android.support.v7.widget.Toolbar>
编码愉快:)
我正在尝试添加一个自定义图像作为操作栏徽标,这可行,但是,当它被添加到操作栏时,此图像前面有大约 30-50dp 的填充。
图像本身没有左侧填充,因此它应该几乎紧挨着屏幕边缘。
这个填充在运行时是从哪里添加的?这是我用于操作栏的自定义布局的代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/actionbar">
<ImageView
android:id="@+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/logo"
</RelativeLayout>
这是我在 activity:
中设置操作栏布局的代码 ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
LayoutInflater inflator = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.actionbar, null);
actionBar.setCustomView(v);
我发现删除此填充的唯一方法是将 android:layout_marginLeft="-30dp"
添加到自定义操作栏的布局中,但我绝对不认为这是正确的方法。
知道为什么我的图片会有额外的填充吗?在此先感谢您的帮助!
编辑
我设法解决了这个问题,我在 activity 的操作栏设置中添加了以下两行:
Toolbar parent = (Toolbar) v.getParent();
parent.setContentInsetsAbsolute(0, 0);
尝试修改 styles.xml
<style name="MyActionBarTheme" parent="Widget.AppCompat.ActionBar">
<item name="contentInsetStart">0dp</item>
<item name="contentInsetEnd">0dp</item>
</style>
然后就是你的App Theme
<item name="android:actionBarStyle">@style/MyActionBarTheme</item>
或 如果您正在使用 ToolbarLayout
<android.support.v7.widget.Toolbar
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:paddingLeft="0dp">
<!--rest of content -->
</android.support.v7.widget.Toolbar>
编码愉快:)