18

Ich habe die folgende Struktur in der Layout-Datei meines Android-Studio-Projekts, und ich sehe unerklärlichen linken Abstand zwischen dem übergeordneten Element (Toolbar) und seine unmittelbaren Kind-Element (LinearLayout).Unerklärliche Lücke/Padding auf der linken Seite, zwischen Toolbar und LinearLayout

Layout Text

<Toolbar 
    android:layout_width="fill_parent" 
    android:layout_height="600dp" 
    android:id="@+id/toolbar" 
    android:background="#313B45" 
    android:weightSum="1"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" android:orientation="vertical"> 
     <ImageView 
      android:id="@+id/headerimage" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:scaleType="fitXY" 
      android:layout_gravity="left|top" 
      android:layout_weight="1"/> 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:text="New Text" 
      android:id="@+id/textView" 
      android:scaleType="fitXY" 
      android:layout_gravity="left|top" 
      android:layout_weight="1"/> 

    </LinearLayout> 
</Toolbar> 

Wie kann ich diese Lücke entfernen und haben das Kind Linearlayout ausrichten vollständig mit der Mutter Toolbar?

Antwort

35

diese Zeilen in Ihre Symbolleiste Layout hinzufügen: Für API < = 21 Symbolleiste:

app:contentInsetLeft="0dp" 
    app:contentInsetStart="0dp" 

Für API 21> = Symbolleiste:

android:contentInsetLeft="0dp" 
    android:contentInsetStart="0dp" 

Der linke Einsatz von Toolbar contentInsetStart verursacht wird, die Standardmäßig ist 16dp.

Hier ist der vollständige Code:

<android.support.v7.widget.Toolbar 
android:layout_width="fill_parent" 
android:layout_height="600dp" 
android:id="@+id/toolbar" 
android:background="#313B45" 
android:weightSum="1" 
app:contentInsetLeft="0dp" 
app:contentInsetStart="0dp" 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <ImageView 
     android:id="@+id/headerimage" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:scaleType="fitXY" 
     android:layout_gravity="left|top" 
     android:layout_weight="1" /> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:text="New Text" 
     android:id="@+id/textView" 
     android:scaleType="fitXY" 
     android:layout_gravity="left|top" 
     android:layout_weight="1" /> 

</LinearLayout> 

+0

Versuchen Sie, dies in Ihrem build.gradle: compile 'com.android.support:appcompat-v7:21.0.3' –

+0

Vielen Dank. Ich hatte das Präfix "app:" hinzugefügt, als ich API 21 verwendete. Die Verwendung von "android.support.v7.widget.Toolbar" gibt mir immer einen Laufzeitfehler beim Aufblasen der Symbolleiste. Danke für die Antwort! – stackoverflowN

+3

Sie sind ein Lebensretter! – yUdoDis

1

Die obige Antwort half nur einen Teil zu lösen, fügen Sie diese Zeilen und es sollte in Ordnung

android.support.v7.widget.Toolbar 
     xmlns:app="schemas.android.com/apk/res-auto" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/primaryColor" 
     android:contentInsetLeft="0dp" 
     android:contentInsetStart="0dp" 
     app:contentInsetLeft="0dp" 
     app:contentInsetStart="0dp" 
     android:contentInsetRight="0dp" 
     android:contentInsetEnd="0dp" 
     app:contentInsetRight="0dp" 
     app:contentInsetEnd="0dp" /> 

Hinweis, dass android arbeiten: contentInsetLeft und App: contentInsetLeft sind 2 verschiedene Dinge und beide benötigt werden

Verwandte Themen