简体   繁体   English

如何在Android中的ActionBar中删除左填充边距的额外空间?

[英]How to remove the left padding margin extra space in the ActionBar in Android?

I've been pulling my hairs against this left padding/margin just don't go away issue, about to become bald... A solution to this would be much appreciated!我一直在拉我的头发反对这个左边的填充/边距只是不要消失问题,即将成为秃头......对此的解决方案将不胜感激! I've tried these answers from SO, it did moved some of the padding, but not all of them.我已经尝试过 SO 的这些答案,它确实移动了一些填充,但不是全部。 Basically all the answers says to set the contentInsetStart and contentInsetLeft to 0dp.基本上所有的答案都说将 contentInsetStart 和 contentInsetLeft 设置为 0dp。 This does remove some of the padding on the left, but I still see a padding, it's just smaller than before I set the contentInsetStart and contentInsetLeft to 0dp.这确实删除了左侧的一些填充,但我仍然看到一个填充,它只是比我将 contentInsetStart 和 contentInsetLeft 设置为 0dp 之前小。 This solution works on Samsung phones, but not on any tablets I've tested, such as Nexus 7, Nexus 9, Dell Venus 8, etc.此解决方案适用于三星手机,但不适用于我测试过的任何平板电脑,例如 Nexus 7、Nexus 9、Dell Venus 8 等。 在此处输入图片说明

I believe a lot of people had this issue and these are the answers I've tried but still leaving a padding/margin on the left shown above.我相信很多人都有这个问题,这些是我尝试过的答案,但仍然在上面显示的左侧留下填充/边距。

Android: remove left margin from actionbar's custom layout Android:从操作栏的自定义布局中删除左边距

Android Lollipop, AppCompat ActionBar custom view doesn't take up whole screen width Android Lollipop、AppCompat ActionBar 自定义视图不占用整个屏幕宽度

Android API 21 Toolbar Padding Android API 21 工具栏填充

Padding/margin on the left side of my activity/actionbar - dont know where it comes from, cannot remove it 我的活动/操作栏左侧的填充/边距 - 不知道它来自哪里,无法删除它

How to remove default layout's padding/margin 如何删除默认布局的填充/边距

How to remove the margin between the app icon and the edge of the screen on the ActionBar? 如何在 ActionBar 上移除应用程序图标和屏幕边缘之间的边距?

The style样式

<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>

The activity layout, activity_main.xml, it's pretty much empty, just a dummy layout for the Main activity class, the custom action bar view is in another layout file.活动布局,activity_main.xml,它几乎是空的,只是主要活动类的虚拟布局,自定义操作栏视图位于另一个布局文件中。

<?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>

Now, here is the custom actionbar layout现在,这是自定义操作栏布局

<?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>

The MainActivity.java which inflates the custom actionbar layout and set it mActionBar.setCustomView(mCustomView); MainActivity.java 膨胀自定义操作栏布局并将其设置为 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();
            }
        });
    }
}

The sample app with this issue can be found here可以在此处找到存在此问题的示例应用程序

You should use a ToolBar instead of an ActionBar and call setContentInsetsAbsolute or if you really want to keep ActionBar, you should do it like that : 你应该使用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);

Haven't tried the Toolbar setContentInsetsAbsolute function because it only works on API 21 or higher. 尚未尝试使用工具栏setContentInsetsAbsolute函数,因为它仅适用于API 21或更高版本。 Minimum target of my app is API 15 . 我的应用程序的最低目标是API 15。

User Nilesh Senta's solution on this same ( extra margin issue ) was to update the style and it worked for me! 用户Nilesh Senta在这方面的解决方案( 额外保证金问题 )是更新风格,它对我有用! Below is my code 以下是我的代码

styles.xml 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) 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); 

If the navigation button is present, contentInsetsAbsolute is not the solution, or is not enough at the very least.如果存在导航按钮,则 contentInsetsAbsolute 不是解决方案,或者至少是不够的。 Try the following:请尝试以下操作:

val toolbar = customSearch.parent as Toolbar
toolbar.setContentInsetStartWithNavigation(0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM