【发布时间】:2016-07-24 17:57:50
【问题描述】:
我一直在努力解决这个左边的填充/边距问题,只是不会消失,即将变得秃顶......对此的解决方案将不胜感激!我已经从 SO 尝试了这些答案,它确实移动了一些填充,但不是全部。基本上所有的答案都说将 contentInsetStart 和 contentInsetLeft 设置为 0dp。这确实删除了左侧的一些填充,但我仍然看到一个填充,它比我将 contentInsetStart 和 contentInsetLeft 设置为 0dp 之前要小。此解决方案适用于三星手机,但不适用于我的任何平板电脑经测试,如 Nexus 7、Nexus 9、Dell Venus 8 等。
我相信很多人都有这个问题,这些是我尝试过的答案,但仍然在上面显示的左侧留下了填充/边距。
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
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_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 扩展自定义操作栏布局并设置它 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 试过了,它给出了同样的问题。
-
所以如果工具栏或操作栏没有按照您的意愿行事,您可以创建一个类似工具栏的布局(可以使用RelativeLayout)并用作工具栏
-
是的,这将是我最后的选择,但我想知道是否有使用操作栏或工具栏的解决方案,或者这是不可能的。
-
我在 Android M 上遇到了同样的问题(仅在平板电脑上)。 @s-hunter 你找到解决办法了吗?
标签: android layout android-actionbar margin padding