[OFFEN] Constraint: Buttons gleichmäßig untereinander ausrichten

  • 2 Antworten
  • Letztes Antwortdatum
A

androidcoder

Neues Mitglied
0
Hallo,
ich habe zwei Fragen:

1) Warum kann ich nicht das focusable und focusableInTouchMode deaktivieren(siehe Screenshot)?
2) Wie schaffe ich es, dass die Buttons alle untereinander gleichmäßig ausgerichtet sind? Die Constraints habe ich meiner Meinung nach, richtig gesetzt. Hier der Code.

Code:
<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.admin.calculator.MainActivity">

    <EditText
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:editable="false"
        android:ems="10"
        android:focusedByDefault="false"
        android:inputType="numberSigned|numberDecimal"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:focusableInTouchMode="false" />

    <TextView
        android:id="@+id/operation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintBaseline_toBaselineOf="@+id/newNumber"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/newNumber" />

    <EditText
        android:id="@+id/newNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:inputType="numberSigned|numberDecimal"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/result" />

    <Button
        android:id="@+id/button0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:minWidth="48dp"
        android:text="0"
        app:layout_constraintLeft_toLeftOf="@+id/button1"
        app:layout_constraintTop_toBottomOf="@+id/button1" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:minWidth="48dp"
        android:text="1"
        app:layout_constraintLeft_toLeftOf="@+id/button4"
        app:layout_constraintTop_toBottomOf="@+id/button4" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:minWidth="48dp"
        android:text="2"
        app:layout_constraintBaseline_toBaselineOf="@+id/button1"
        app:layout_constraintLeft_toRightOf="@+id/button1" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:minWidth="48dp"
        android:text="3"
        app:layout_constraintBaseline_toBaselineOf="@+id/button2"
        app:layout_constraintLeft_toRightOf="@+id/button2" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:minWidth="48dp"
        android:text="4"
        app:layout_constraintLeft_toLeftOf="@+id/button7"
        app:layout_constraintTop_toBottomOf="@+id/button7" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:minWidth="48dp"
        android:text="5"
        app:layout_constraintBaseline_toBaselineOf="@+id/button4"
        app:layout_constraintLeft_toRightOf="@+id/button4" />

    <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:minWidth="48dp"
        android:text="6"
        app:layout_constraintBaseline_toBaselineOf="@+id/button5"
        app:layout_constraintLeft_toRightOf="@+id/button5" />

    <Button
        android:id="@+id/button7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:minWidth="48dp"
        android:text="7"
        android:layout_marginTop="7dp"
        app:layout_constraintTop_toBottomOf="@+id/newNumber"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="@+id/newNumber" />

    <Button
        android:id="@+id/button8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:minWidth="48dp"
        android:text="8"
        app:layout_constraintBaseline_toBaselineOf="@+id/button7"
        app:layout_constraintLeft_toRightOf="@+id/button7" />

    <Button
        android:id="@+id/button9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:minWidth="48dp"
        android:text="9"
        app:layout_constraintBaseline_toBaselineOf="@+id/button8"
        app:layout_constraintLeft_toRightOf="@+id/button8" />

    <Button
        android:id="@+id/buttonDivide"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:minWidth="48dp"
        android:text="/"
        app:layout_constraintBaseline_toBaselineOf="@+id/button9"
        app:layout_constraintLeft_toRightOf="@+id/button9" />

    <Button
        android:id="@+id/buttonMultiply"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:minWidth="48dp"
        android:text="*"
        app:layout_constraintBaseline_toBaselineOf="@+id/button6"
        app:layout_constraintLeft_toRightOf="@+id/button6" />

    <Button
        android:id="@+id/buttonMinus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:minWidth="48dp"
        android:text="-"
        app:layout_constraintBaseline_toBaselineOf="@+id/button3"
        app:layout_constraintLeft_toRightOf="@+id/button3" />

    <Button
        android:id="@+id/buttonPlus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:minWidth="48dp"
        android:text="+"
        app:layout_constraintBaseline_toBaselineOf="@+id/buttonEquals"
        app:layout_constraintLeft_toRightOf="@+id/buttonEquals" />

    <Button
        android:id="@+id/buttonEquals"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:minWidth="48dp"
        android:text="="
        app:layout_constraintBaseline_toBaselineOf="@+id/buttonDot"
        app:layout_constraintLeft_toRightOf="@+id/buttonDot" />

    <Button
        android:id="@+id/buttonDot"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:minWidth="48dp"
        android:text="."
        app:layout_constraintBaseline_toBaselineOf="@+id/button0"
        app:layout_constraintLeft_toRightOf="@+id/button0" />

</android.support.constraint.ConstraintLayout>
 

Anhänge

  • 2.PNG
    2.PNG
    18 KB · Aufrufe: 238
  • Danke
Reaktionen: androidcoder
Zu deiner ersten Frage:
Versuch mal, deinem MainLayout (Constraint) folgendes hinzuzufügen:
Code:
android:focusableInTouchMode="true"

Eventuell die focus-Einträge vom EditText vorm Testen mal auskommentieren. So müsste der Focus beim Starten der Activity auf deinem Layout liegen und nicht direkt auf dem EditText-Objekt.


Auf die zweite Frage bezogen: Du könntest auch für die jeweiligen Zeilen ein HorizontalLayout verwenden. Kardroids Antwort sollte aber genauso gut funktionieren. Als kleine Info falls du das noch nicht wusstest: Du kannst verschiedene Layouts ineinander verschachteln. Du bist also nicht gezwungen, nur dein "Hauptlayout" (in dem Fall das Constraint) zu verwenden.

Etwa so:
Code:
ConstraintLayout
   HorizontalLayout
      Button1
      Button2
   HorizontalLayout
      Button3
      Button4
usw usw..
 
Zuletzt bearbeitet:
  • Danke
Reaktionen: androidcoder

Ähnliche Themen

A
Antworten
11
Aufrufe
389
swa00
swa00
A
  • AnimaAngelo85
Antworten
1
Aufrufe
536
swa00
swa00
MES
Antworten
10
Aufrufe
1.153
MES
MES
Zurück
Oben Unten