我有一些关于安卓工具栏的问题。通常,如果我将自定义 View 设置到工具栏中,该 View 应该从左到右填满整个工具栏空间并且没有边距。
但是我的左边有一个空格,这些是我的代码:
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/base_toolbar"
android:layout_width="match_parent"
android:layout_height="46dip"
android:background="?attr/colorPrimary" />
<FrameLayout
android:id="@+id/base_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Activity :
private void initToolbar() {
toolbar = (Toolbar) findViewById(R.id.base_toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if( actionBar != null)
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
contentLayout = (FrameLayout) findViewById(R.id.base_content);
}
我的代码有什么问题还是应该是这样的?
最佳答案
左边的空白是由Toolbar的contentInsetStart造成的,默认是16dp。您可以将其设置为 0 以将其删除。不要忘记添加架构。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/base_toolbar"
android:layout_width="match_parent"
android:layout_height="46dip"
android:background="?attr/colorPrimary"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp" />
<FrameLayout
android:id="@+id/base_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
关于android - Android Toolbar添加自定义 View 时,会有一个marginLeft,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32086148/