2016-08-03 15 views
4

Ich benutze das neue Layout Constraint, um mein Layout zu erstellen. Ich brauche Button, die fast die gesamte Bildschirmbreite einnimmt und das war einfach mit Einschränkungen.Constraint Layout Button Text Zentrum Ausrichtung

enter image description here

Wie Sie im Bild sehen, kann ich die Breite 0dp (jeder Größe) setze, aber der Text kleben nicht in der Mitte, was in der Regel ist das normal für eine Schaltfläche Text.

Ich habe versucht: - Set Schwere zum Zentrum - Set Textalignment

zum Zentrum wie folgt aussieht Eigenschaften nicht mit 0dp Breite (jede Größe) arbeiten können.

Also habe ich versucht, die Breite auf match_parent mit Schwerpunkt zu setzen.

enter image description here

Es ist ein wenig nach rechts.

Kann jemand dieses Verhalten reparieren?

Bitte beachte, dass ich alpha4 bin mit

compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha4'

XML-Code

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/content_login" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    tools:context="br.com.marmotex.ui.LoginActivityFragment" 
    tools:showIn="@layout/activity_login"> 

    <Button 
     android:text="Log in" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:id="@+id/btLogin" 
     android:layout_marginTop="48dp" 
     app:layout_constraintTop_toBottomOf="@+id/textView6" 
     android:layout_marginEnd="16dp" 
     app:layout_constraintRight_toRightOf="@+id/content_login" 
     android:layout_marginRight="16dp" 
     android:layout_marginStart="16dp" 
     app:layout_constraintLeft_toLeftOf="@+id/content_login" 
     android:layout_marginLeft="16dp" 
     android:textColor="@android:color/white" 
     android:background="@color/BackgroundColor" /> 

</android.support.constraint.ConstraintLayout> 

EDIT Es war ein Fehler in ConstraintLayout alpha4.

UPDATE Google alpha5 freigegeben hat, funktioniert der obige Code jetzt. Release note

Antwort

5

Es ist ein wenig nach rechts.

Ich denke Rand (e) verursacht diese. Und es betrifft nicht nur Tasten, nach meiner Erfahrung. Die Marge schraubt auch TextInputEditText.

Unten ist ein funktionierender Code, aber bitte beachten Sie android:layout_width="match_parent" auf der Schaltfläche. Jedes Mal, wenn ich im Editor klicke, wird es in android:layout_width="0dp" geändert und die Tastenausrichtung zerstört.

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity"> 


    <Button 
     android:id="@+id/button_survey" 
     android:layout_width="match_parent" 
     android:layout_height="52dp" 
     android:text="Button" 
     app:layout_constraintBottom_toBottomOf="@+id/activity_main" 
     app:layout_constraintLeft_toLeftOf="@+id/activity_main" 
     app:layout_constraintRight_toRightOf="@+id/activity_main" 
     app:layout_constraintTop_toTopOf="@+id/activity_main" 
     tools:text="@string/main_activity_btn_survey" 
     android:layout_marginEnd="8dp" 
     android:layout_marginRight="8dp" 
     android:layout_marginStart="8dp" 
     android:layout_marginLeft="8dp" /> 


</android.support.constraint.ConstraintLayout> 

Inspiriert von Hobo Joe Lösung, unten ist die Art, wie ich es getan habe. Seine Lösung funktioniert, muss aber immer noch mit Abstand zum Erstellen von Abständen verwendet werden. Wenn stattdessen ein Rand verwendet wurde, wird die Ausrichtung des Schaltflächen-Textes leicht nach rechts verschoben. Also habe ich Padding auf LinearLayout (oder ConstraintLayout) anstelle von Rand auf Knopf verwendet.

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
    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:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity"> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:layout_constraintLeft_toLeftOf="@+id/activity_main" 
     app:layout_constraintTop_toTopOf="@+id/activity_main" 
     app:layout_constraintRight_toRightOf="@+id/activity_main" 
     app:layout_constraintBottom_toBottomOf="@+id/activity_main" 
     android:padding="16dp"> 
     <Button 
      android:text="Button" 
      android:layout_width="match_parent" 
      android:layout_height="52dp" 
      android:id="@+id/button_survey" 
      android:layout_weight="1" 
      tools:text="@string/main_activity_btn_survey" 
      /> 
    </LinearLayout> 
</android.support.constraint.ConstraintLayout> 
+1

Google hat Alpha5 veröffentlicht, es aktualisiert und Sie werden diese Arbeit nicht mehr benötigen. –

+0

danke @ClaytonOliveira. Kann ich meine Antwort aktualisieren? –

0

Dies ist ein Fehler. Sie können jedoch umgehen, indem Sie die Schaltfläche innerhalb eines LinearLayout (oder einer anderen Standard-ViewGroup) platzieren. Legen Sie die übergeordnete Ansicht und die Breite der Schaltfläche auf match_parent fest, und verschieben Sie alle Einschränkungen, die Sie an der Schaltfläche auf das übergeordnete Layout hatten.

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:layout_constraintLeft_toLeftOf="@+id/parent_left" 
    app:layout_constraintTop_toTopOf="@+id/parent_top" 
    app:layout_constraintRight_toRightOf="@+id/parent_right"> 

    <Button 
     android:id="@+id/test" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Centered Text"/> 

</LinearLayout> 
+0

Ich tat, was Sie vorgeschlagen, aber nicht funktioniert. –

+0

Können Sie Ihren Layout-Code veröffentlichen? Ich habe gerade mit dem XML getestet, das ich gepostet habe, und es hat funktioniert. –

+0

Code anzeigen hinzugefügt! –

0

Nun denke ich sein wegen dieser Einschränkungen App: layout_constraintRight_toRightOf App: layout_constraintLeft_toLeftOf

Ihren aktuellen Knopf mit diesem ersetzen:

<Button 
    android:text="Log in" 
    android:layout_width="match_parent" 
    android:layout_height="48dp" 
    android:id="@+id/btLogin" 
    android:textColor="@android:color/white" 
    android:background="@color/BackgroundColor" 
    android:gravity="center" 
    android:textAlignment="center" 
    android:layout_marginTop="100dp" 
    tools:layout_editor_absoluteX="-1dp" 
    app:layout_constraintTop_toBottomOf="@+id/textView6" /> 

Hope this helfen.

3

Haben Sie es versucht?

android:textAlignment="center" 

Das funktioniert für mich.

Verwandte Themen