2013-04-24 1 views
21

Ich möchte Ränder prozentual einstellen .. Ich habe 4 Bildansichten in einem linearen Layout und möchten Standard linken, rechten, oberen, unteren Rand, die gleichen Prozentsatz für jede Bildschirmgröße zu halten.android Layout Ränder mit Prozentsatz

ist es möglich?

hier ist eine Demo, was ich will .. enter image description here

Und hier ist, was ich versucht habe und nicht

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:weightSum="10" > 

     <Thumbnail 
      android:id="@+id/thumb1" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="4" /> 

     <Thumbnail 
      android:id="@+id/thumb2" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="4" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:weightSum="10" > 

     <Thumbnail 
      android:id="@+id/thumb3" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="4" > 
     </Thumbnail> 

     <Thumbnail 
      android:id="@+id/thumb4" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="4" /> 

    </LinearLayout> 

</LinearLayout> 

Vielen Dank für Ihre Hilfe kann

Antwort

22

Sie haben nicht funktioniert unsichtbar View s in Ihrem LinearLayout s als Abstandhalter und verwenden Sie den layout_weight Mechanismus, um ihnen relative Größe zuzuordnen.

Beispiel:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1"> 

    <Thumbnail 
     android:id="@+id/thumb1" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="4" /> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_weight="2" 
     android:visibility="invisible"/> 

    <Thumbnail 
     android:id="@+id/thumb2" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="4" /> 

</LinearLayout> 
+1

logisch, wenn es keine andere Möglichkeit gibt :) aber mit Speichernutzung nicht sicher, weil ich so viel unsichtbar Ansichten benötigen – dracula

+2

Es gibt eine '' Ansicht dafür. Ich denke nicht, dass es eine Performance-/Memory-Strafe für die Verwendung von unsichtbarem 'View' geben wird, aber wirklich nicht sicher -' View.java' ist riesig, hat es nur für einige Minuten überflogen. –

+1

@AlexanderMalakhov 'Space' wurde in API-Ebene 14 hinzugefügt, so dass es nicht so nützlich ist. Seine Benennung macht den Zweck jedoch deutlicher. – laalto

0

an Ihrem Bild hier schauen ist, was man tun könnte, ein relatives Layout verwenden und das lineare Layout im Innern setzen. Sie können auch die Gewichtssumme verwenden, wenn Sie die Bildansichten so anpassen möchten, dass sie richtig passen.

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_marginLeft="5dp" 
    android:layout_marginLeft="5dp" > 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:orientation="vertical" > 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:orientation="vertical" > 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
    </LinearLayout> 

</RelativeLayout> 
+0

geben genaue Marge ist nicht gut für mehrere Bildschirme. Deshalb suche ich nach einer percentege Lösung – dracula

+0

Das Setzen eines linken und rechten Randes für Ihr relatives Layout wird die gleichen Ränder auf allen Bildschirmen setzen, da diese Werte in dp sind. – deepdroid

+0

aber dp-Werte Auswirkungen für jede Dichte unterschiedlich. – dracula

19

Hinzugefügt am 1. August 2017:

Die beiden Layouts aus dieser Antwort sind jetzt veraltet, aber es ist eine Beschreibung zur Verfügung gestellt dort, wie mit ConstraintLayout die gleichen Funktionalität zu erhalten. Danke an dpg für das hinweisend.

Wenn Sie die Prozentsätze mit Ressourcen verwenden möchten, könnte this answer nützlich sein.


Alte Antwort:

Es gibt jetzt eine bessere Art und Weise, die 23.0.0 mit Support-Bibliothek Version herauskam (etwa Zeit, nicht wahr?). Sie können jetzt PercentFrameLayout oder PercentRelativeLayout verwenden. Beide haben die folgenden Attribute:

  • layout_widthPercent
  • layout_heightPercent
  • layout_marginPercent
  • layout_marginLeftPercent
  • layout_marginTopPercent
  • layout_marginRightPercent
  • layout_marginBottomPercent
  • layout_marginStartPe rcent
  • layout_marginEndPercent

Sie auch einen Blick auf PercentLayoutHelper.PercentLayoutParams

+0

Ist das nur für Android 6? –

+1

Die prozentualen Layouts befinden sich in der Bibliothek für die prozentuale Unterstützung, was bedeutet, dass sie nicht nur für Android 6 geeignet sind. – neits

+1

Der neue bessere Weg ist jetzt veraltet. Für beide Layouts auf den Links: 'Diese Klasse wurde in API-Ebene 26.0.0-Beta1 veraltet. Verwenden Sie stattdessen ConstraintLayout und die zugehörigen Layouts. " – dpg

0

nehmen Sie Ränder mit Prozentsatz ConstraintLayout's Guidelines Verwendung einstellen.

Sagen Sie bitte folgende prozentuale Werte für Ihr Layout definieren möchten:

Requested percentage values

Dazu fügen Sie einfach folgende Richtlinien zum Layout:

Layout Editor

<?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" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:scaleType="centerCrop" 
     app:srcCompat="@drawable/ic_launcher_background" 
     app:layout_constraintTop_toTopOf="@+id/horGuideline1" 
     app:layout_constraintStart_toStartOf="@+id/verGuideline1" 
     app:layout_constraintEnd_toStartOf="@+id/verGuideline2" 
     app:layout_constraintBottom_toTopOf="@+id/horGuideline2" /> 

    <ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:scaleType="centerCrop" 
     app:srcCompat="@drawable/ic_launcher_background" 
     app:layout_constraintTop_toTopOf="@+id/horGuideline1" 
     app:layout_constraintStart_toStartOf="@+id/verGuideline3" 
     app:layout_constraintEnd_toStartOf="@+id/verGuideline4" 
     app:layout_constraintBottom_toTopOf="@+id/horGuideline2" /> 

    <ImageView 
     android:id="@+id/imageView3" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:scaleType="centerCrop" 
     app:srcCompat="@drawable/ic_launcher_background" 
     app:layout_constraintTop_toTopOf="@+id/horGuideline3" 
     app:layout_constraintStart_toStartOf="@+id/verGuideline1" 
     app:layout_constraintEnd_toStartOf="@+id/verGuideline2" 
     app:layout_constraintBottom_toTopOf="@+id/horGuideline4" /> 

    <ImageView 
     android:id="@+id/imageView4" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:scaleType="centerCrop" 
     app:srcCompat="@drawable/ic_launcher_background" 
     app:layout_constraintTop_toTopOf="@+id/horGuideline3" 
     app:layout_constraintStart_toStartOf="@+id/verGuideline3" 
     app:layout_constraintEnd_toStartOf="@+id/verGuideline4" 
     app:layout_constraintBottom_toTopOf="@+id/horGuideline4" /> 

    <android.support.constraint.Guideline 
     android:id="@+id/verGuideline1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     app:layout_constraintGuide_percent="0.05" /> 

    <android.support.constraint.Guideline 
     android:id="@+id/verGuideline2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     app:layout_constraintGuide_percent="0.35" /> 

    <android.support.constraint.Guideline 
     android:id="@+id/verGuideline3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     app:layout_constraintGuide_percent="0.65" /> 

    <android.support.constraint.Guideline 
     android:id="@+id/verGuideline4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     app:layout_constraintGuide_percent="0.95" /> 

    <android.support.constraint.Guideline 
     android:id="@+id/horGuideline1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     app:layout_constraintGuide_percent="0.12" /> 

    <android.support.constraint.Guideline 
     android:id="@+id/horGuideline2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     app:layout_constraintGuide_percent="0.47" /> 

    <android.support.constraint.Guideline 
     android:id="@+id/horGuideline3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     app:layout_constraintGuide_percent="0.53" /> 

    <android.support.constraint.Guideline 
     android:id="@+id/horGuideline4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     app:layout_constraintGuide_percent="0.88" /> 

</android.support.constraint.ConstraintLayout> 

Als Ergebnis Ihr Layout sieht ungefähr so ​​aus:

Result view