2017-07-10 2 views
5

Ich habe ein Layout, das wie folgt aussieht:Wie setze ich eine maximale Breite für eine Ansicht in einem Android constraintLayout?

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayoutxmlns: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.plarke.proto1.QuizActivity"> 

<Button 
    android:id="@+id/compare_settings" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Button" 
    app:layout_constraintTop_toTopOf="parent" 
    android:layout_marginTop="16dp" 
    android:layout_marginRight="16dp" 
    app:layout_constraintRight_toRightOf="parent" 
    android:layout_marginEnd="16dp" /> 

<TextView 
    android:id="@+id/compare_score" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="TextView" 
    app:layout_constraintTop_toTopOf="parent" 
    android:layout_marginTop="16dp" 
    android:layout_marginLeft="16dp" 
    app:layout_constraintLeft_toLeftOf="parent" 
    android:layout_marginStart="16dp" /> 

<SeekBar 
    android:id="@+id/compare_pitchbar" 
    android:layout_width="0dp" 
    android:layout_height="23dp" 
    android:layout_marginBottom="50dp" 
    android:layout_marginEnd="16dp" 
    android:layout_marginLeft="16dp" 
    android:layout_marginRight="16dp" 
    android:layout_marginStart="16dp" 
    android:progressBackgroundTint="@color/colorAccent" 
    android:progressBackgroundTintMode="src_over" 
    app:layout_constraintBottom_toTopOf="@+id/textView" 
    app:layout_constraintHorizontal_bias="0.0" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" /> 

<Button 
    android:id="@+id/button2" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="16dp" 
    android:layout_marginLeft="16dp" 
    android:layout_marginRight="16dp" 
    android:text="Button" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    android:layout_marginStart="16dp" 
    android:layout_marginEnd="16dp" /> 

<TextView 
    android:id="@+id/textView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="50dp" 
    app:layout_constraintBottom_toTopOf="@+id/button2" 
    tools:text="The seekbar is all the way to the left." 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintLeft_toLeftOf="parent" 
    android:layout_marginRight="0dp" 
    android:layout_marginLeft="0dp" /> 

Und ich will, es ändern, so dass die seekBar standardmäßig eine bestimmte Breite, aber wenn der Bildschirm nicht ist breit genug, füllt es nur den Bildschirm (mit Rändern). Ich habe versucht, layout_width zu setzen, aber dann geht es einfach vom Bildschirm, wenn es zu weit ist. Mit max_width wird überhaupt nichts gemacht. Was ist möglich in einem ConstrainstLayout? Wenn nicht, was sollte ich verwenden?

+0

Sie verschiedene Layouts für verschiedene Bildschirmauflösungen verwenden sollten. –

Antwort

19

Ich denke, was Sie suchen, ist matchConstraintMaxWidth Attribut.

Wenn Sie es zu Ihrem SeekBar mit einem Wert von, sagen sie, 200dp hinzufügen, an die Mutter linke und rechte Randbedingungen zu halten und eine Breite von 0dp, wird es die Breite der Ansicht strecken, um zu 200dp aber wenn es mehr Raum es wird bei 200dp bleiben.

so sollten Sie Ihre xml wie folgt aussehen:

<SeekBar 
    ... 
    android:layout_width="0dp" 
    app:layout_constraintStart_toStartOf="parent" 
    app:layout_constraintEnd_toEndOf="parent" 
    app:layout_constraintWidth_max="200dp" /> 

Sie auch in der Lage sein sollte, um es einrichten programmatisch mit

ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(0, 0); 
layoutParams.matchConstraintMaxWidth = ... 
+0

das ist also ein Feld in der ConstraintLayout.LayoutParams-Klasse? Muss es zur Laufzeit eingestellt werden? – pjc

+0

korrekt, es ist ein Feld von ConstraintLayout.LayoutParams, ich stelle vor es kann über Java zu – lelloman

+0

gesetzt werden Ich denke, das ist ein schlechter Kommentar, wie es für alle Bildschirme erforderlich ist, um den Wert zu ändern – Morozov

Verwandte Themen