我一直在为这个左填充/边距问题而苦恼,几乎要变成秃头了...非常感谢您能提供解决方案!我尝试了SO上的这些答案,它确实移动了一些填充,但并没有全部移除。基本上所有的答案都建议将contentInsetStart和contentInsetLeft设置为0dp。这确实从左侧移除了一些填充,但我仍然看到有填充存在,只不过比我将contentInsetStart和contentInsetLeft设置为0dp之前小了一点。这种解决方案适用于三星手机,但对我测试过的任何平板电脑(如Nexus 7、Nexus 9、Dell Venus 8等)都不起作用。
我相信很多人都遇到了这个问题,我已经尝试了这些答案,但仍然留下了上面显示的左侧填充/边距。
Android:从操作栏的自定义布局中删除左边距
活动布局activity_main.xml基本上是空的,只是Main活动类的虚拟布局,自定义操作栏视图在另一个布局文件中。
现在,这是自定义的操作栏布局。
![enter image description here](https://istack.dev59.com/Ahsnb.webp)
Android Lollipop, AppCompat ActionBar自定义视图不占用整个屏幕宽度
我的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();
}
});
}
}
这个问题的示例应用程序可以在这里找到