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

标签 android layout android-actionbar margin padding

我一直在为这个左边的填充/边距拉扯我的头发只是不要消失问题,即将变成秃头......对此的解决方案将不胜感激!我已经尝试了 SO 的这些答案,它确实移动了一些填充,但不是全部。基本上所有的答案都说 将 contentInsetStart 和 contentInsetLeft 设置为 0dp。这确实删除了左侧的一些填充,但我仍然看到填充,它只是比我将 contentInsetStart 和 contentInsetLeft 设置为 0dp 之前小。此解决方案适用于三星手机,但不适用于我的任何平板电脑经测试,如Nexus 7、Nexus 9、Dell Venus 8等。 enter image description here

我相信很多人都遇到过这个问题,这些是我尝试过的答案,但仍然在上面显示的左侧留下了填充/边距。

Android: remove left margin from actionbar's custom layout

Android Lollipop, AppCompat ActionBar custom view doesn't take up whole screen width

Android API 21 Toolbar Padding

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?

风格

<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 布局,activity_main.xml,几乎是空的,只是主要 Activity 类的虚拟布局,自定义操作栏 View 在另一个布局文件中。

<?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 扩展自定义操作栏布局并设置它 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();
            }
        });
    }
}

可以找到存在此问题的示例应用程序 here

最佳答案

你应该使用 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);

关于android - 如何在 Android 中删除 ActionBar 中的左填充边距额外空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36415424/

相关文章:

GWT 布局令人费解

ruby-on-rails - Rails从before_filter方法中设置布局

android - 如何在 Android 中设置自定义搜索建议列表的样式

Android:在一个 Activity 中向操作栏添加样式

android - 如何直接使用 ArrayAdapter 更改对象属性? (列表显示)

android - 风格没有得到应用

layout - CSS:具有固定中心的可变宽度侧面板

java - 恢复操作栏

android - Android 的 Facebook 自动收报机

android - Google Play Games y GoogleMobileAds 不能在 Unity 5 中协同工作