简体   繁体   English

从工具栏删除边距

[英]Remove margin from Toolbar

I created custom Toolbar for my android app by Google's tutorial, but even thought it seems like my code is same as in tutorial, I have an addional margin at my toolbar. 我通过Google的教程为自己的android应用创建了自定义工具栏,但即使认为自己的代码与教程中的代码相同,我的工具栏也有一个空白。

在我的工具栏上的保证金

Here is my layout.xml 这是我的layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.gruber.jakub.personalscheduler.MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:paddingLeft="0dp"
        android:elevation="4dp"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:textSize="30sp"
        android:layout_centerHorizontal="true"
        android:layout_above="@+id/button"
        android:layout_marginBottom="60dp"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="@string/button1"
        android:layout_centerInParent="true" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:src="@android:drawable/ic_menu_today"
        android:elevation="4dp" />

</RelativeLayout>

and my styles.xml 和我的styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <!--Action bar style-->
    <item name="android:actionBarStyle">@style/ActionBarTheme</item>
</style>

<style name="ActionBarTheme" parent="Widget.AppCompat.ActionBar">
    <item name="android:paddingLeft">0dp</item>
    <item name="android:paddingRight">0dp</item>
    <item name="android:paddingTop">0dp</item>
    <item name="android:paddingBottom">0dp</item>
</style>

It only looks as I want if I change android:padding... property in my layout.xml, but that's not what I would like to do, because I would have to add extra properties to all others elements. 如果我在我的layout.xml中更改android:padding...属性,它只会显示我想要的样子,但这不是我想要的,因为我必须向所有其他元素添加额外的属性。

Simply take a look at your container: 只需看看您的容器:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.gruber.jakub.personalscheduler.MainActivity">

You got padding all over the place! 你到处都是填充物! :

    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"

Those are the margins. 这些就是利润。 Remove them and you are fine. 删除它们,您就可以了。 I suggest you start doing some tutorials. 我建议您开始做一些教程。

The solution is quite simple. 解决方案非常简单。 Firstly, you need to remove the padding definition for your parent RelativeLayout. 首先,您需要删除父级RelativeLayout的填充定义。 And since you want these properties for the rest of the view other than the toobar create a new LinearLayout/RelativeLayout and define the properties there. 而且,由于您希望其余视图的这些属性(除了工具栏之外)都可以创建一个新的LinearLayout / RelativeLayout并在其中定义属性。 Like follows 喜欢如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.gruber.jakub.personalscheduler.MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:paddingLeft="0dp"
        android:elevation="4dp"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"   
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"      
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            android:textSize="30sp"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="60dp"/>

       <Button
           android:id="@+id/button"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:padding="10dp"
           android:text="@string/button1"
           android:layout_centerInParent="true" />

       <android.support.design.widget.FloatingActionButton
          android:id="@+id/fab"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_alignParentRight="true"
          android:src="@android:drawable/ic_menu_today"
          android:elevation="4dp" />
     </RelativeLayout>
</RelativeLayout>

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

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