如何在Android中移除操作栏(ActionBar)中左侧填充边距的额外空间?

5
我一直在为这个左填充/边距问题而苦恼,几乎要变成秃头了...非常感谢您能提供解决方案!我尝试了SO上的这些答案,它确实移动了一些填充,但并没有全部移除。基本上所有的答案都建议将contentInsetStart和contentInsetLeft设置为0dp。这确实从左侧移除了一些填充,但我仍然看到有填充存在,只不过比我将contentInsetStart和contentInsetLeft设置为0dp之前小了一点。这种解决方案适用于三星手机,但对我测试过的任何平板电脑(如Nexus 7、Nexus 9、Dell Venus 8等)都不起作用。 enter image description here 我相信很多人都遇到了这个问题,我已经尝试了这些答案,但仍然留下了上面显示的左侧填充/边距。 Android:从操作栏的自定义布局中删除左边距

Android Lollipop, AppCompat ActionBar自定义视图不占用整个屏幕宽度

Android API 21工具栏填充

我的activity/actionbar左侧的填充/边距-不知道来自哪里,无法删除

如何删除默认布局的填充/边距

如何在ActionBar上删除应用程序图标和屏幕边缘之间的空白边距?

这个样式

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <item name="actionBarStyle">@style/AppThemeActionBar</item>
        <item name="logo">@android:color/transparent</item>
    </style>

    <style name="AppThemeActionBar" parent="@style/Widget.AppCompat.ActionBar">
        <item name="displayOptions">showCustom</item>
        <item name="background">@color/blue</item>
        <item name="actionBarSize">50dp</item>
        <item name="height">50dp</item>

        <item name="contentInsetLeft">0dp</item>
        <item name="contentInsetStart">0dp</item>
        <item name="contentInsetEnd">0dp</item>
        <item name="android:layout_marginLeft">0dp</item>
    </style>
</resources>

活动布局activity_main.xml基本上是空的,只是Main活动类的虚拟布局,自定义操作栏视图在另一个布局文件中。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

</LinearLayout>

现在,这是自定义的操作栏布局。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp" >

    <Button
        android:id="@+id/btn_home"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:background="#ea6464"
        android:text="Home"/>

    <TextView
        android:id="@+id/title_text"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/btn_home"
        android:gravity="center"
        android:textColor="#fff"
        android:textStyle="bold"
        android:text="Hello"/>

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@android:drawable/ic_menu_edit"
        android:background="@null"/>
</RelativeLayout>

MainActivity.java负责填充自定义的actionbar布局并将其设置为mActionBar.setCustomView(mCustomView);

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActionBar mActionBar = getSupportActionBar();
        mActionBar.setDisplayShowHomeEnabled(false);
        mActionBar.setDisplayShowTitleEnabled(false);

        LayoutInflater mInflater = LayoutInflater.from(this);
        View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
        mActionBar.setCustomView(mCustomView);
        mActionBar.setDisplayShowCustomEnabled(true);

        Button btnHome = (Button) findViewById(R.id.btn_home);

        btnHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "Home Button Clicked!", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

这个问题的示例应用程序可以在这里找到


我认为你应该使用工具栏而不是操作栏。 - Norutan
我已经尝试使用工具栏,但问题仍然存在。 - s-hunter
1
如果工具栏或操作栏没有按照您的意愿进行操作,您可以创建一个类似于工具栏的布局(可以使用RelativeLayout),并将其用作工具栏。 - Norutan
是的,那将是我的最后一招,但我想知道是否有使用操作栏或工具栏实现它的解决方案,或者这是不可能的。 - s-hunter
我在Android M(只有平板电脑)上遇到了同样的问题。@s-hunter,你找到任何解决方案了吗? - Samik
不,看起来这只发生在Android API级别低于某个版本的设备上,我的发现是低于或等于API19,我发现有问题的平板电脑都是使用Android API 19。我最终采用的解决方案是根本不使用ActionBar或ToolBar,将其替换为RelativeLayout,并将其放置在主布局文件的顶部。 - s-hunter
3个回答

1
你应该使用ToolBar而不是ActionBar,并调用setContentInsetsAbsolute。如果你真的想保留ActionBar,则应按以下方式操作:
ActionBar mActionBar = getSupportActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);

LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
Toolbar parent = (Toolbar) customView.getParent();
parent.setContentInsetsAbsolute(0,0);

1
我列出的答案确实有使用工具栏的解决方案,我也尝试过了,但仍然无法完全去除左边距。还有像你的答案一样的解决方案,但它也不起作用... - s-hunter

0
如果导航按钮存在,contentInsetsAbsolute并不是解决方案,或者至少不够。请尝试以下方法:
val toolbar = customSearch.parent as Toolbar
toolbar.setContentInsetStartWithNavigation(0)

0

我还没有尝试过Toolbar setContentInsetsAbsolute函数,因为它只适用于API 21或更高版本。我的应用程序的最低目标是API 15。

用户Nilesh Senta在同一个问题(额外边距问题)上提供了解决方案,更新样式后问题得到了解决!以下是我的代码:

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="actionBarStyle">@style/Actionbar</item>
    <item name="android:titleTextStyle">@style/ActionbarTitle</item>
</style>

<style name="Actionbar" parent="Widget.AppCompat.ActionBar">
    <item name="android:titleTextStyle">@style/ActionbarTitle</item>
    <item name="contentInsetStart">0dp</item>
    <item name="contentInsetEnd">0dp</item>
</style>

Java(onCreate)

ActionBar actionBar =  getSupportActionBar();

actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);

ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(
            ActionBar.LayoutParams.MATCH_PARENT,
            ActionBar.LayoutParams.MATCH_PARENT
    );

View view = LayoutInflater.from(this).inflate(R.layout.actionbar_main, null);

actionBar.setCustomView(view, layoutParams); 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接