2015-05-27 6 views
5

Dies ist mein erster Beitrag hier. Ich habe gerade Android-Programmierung gelernt und komme zu diesem Problem. Ich habe ein wenig gesucht und einige Lösungen gefunden, aber ich konnte meinen Code nicht funktionieren lassen. Ich möchte also wissen, welcher Teil meines Codes oder meiner Implementierung falsch ist.Wie man Android-Layout proportional unabhängig von Bildschirmgröße und Ausrichtung machen

Also, das Problem ist, ich möchte ein Layout, das auf verschiedenen Bildschirmgrößen und Ausrichtung bleibt das gleiche Verhältnis bleibt. Hier ist mein Code: „Sie müssen mindestens 10 Ruf Bilder schreiben“

activity_main.xml

<LinearLayout 
 
    xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:tools="http://schemas.android.com/tools" 
 
    android:layout_width="fill_parent" 
 
    android:layout_height="fill_parent" 
 
    android:baselineAligned="false" 
 
    android:orientation="vertical"> 
 

 
    <LinearLayout 
 
     android:layout_width="fill_parent" 
 
     android:layout_height="150dp" > 
 
     <TextView 
 
      android:layout_weight="1" 
 
      android:layout_width="0dp" 
 
      android:layout_height="fill_parent" 
 
      android:gravity="left" 
 
      android:id="@+id/textView1"/> 
 
     <TextView 
 
      android:layout_weight="1" 
 
      android:layout_width="0dp" 
 
      android:layout_height="fill_parent" 
 
      android:gravity="right" 
 
      android:id="@+id/textView2"/> 
 
    </LinearLayout> 
 

 
    <LinearLayout 
 
     android:layout_weight="1" 
 
     android:layout_width="fill_parent" 
 
     android:layout_height="fill_parent" > 
 
     <TextView 
 
      android:layout_weight="1" 
 
      android:layout_width="0dp" 
 
      android:layout_height="fill_parent" 
 
      android:id="@+id/textView3"/> 
 
    </LinearLayout> 
 

 
    <LinearLayout 
 
     android:layout_weight="1" 
 
     android:layout_width="fill_parent" 
 
     android:layout_height="fill_parent" > 
 
     <TextView 
 
      android:layout_weight="1" 
 
      android:layout_width="0dp" 
 
      android:layout_height="fill_parent" 
 
      android:gravity="left" 
 
      android:id="@+id/textView4"/> 
 

 
     <TextView 
 
      android:layout_weight="1" 
 
      android:layout_width="0dp" 
 
      android:layout_height="fill_parent" 
 
      android:gravity="center" 
 
      android:id="@+id/textView5"/> 
 

 
     <TextView 
 
      android:layout_weight="1" 
 
      android:layout_width="0dp" 
 
      android:layout_height="fill_parent" 
 
      android:gravity="right" 
 
      android:id="@+id/textView6"/> 
 
    </LinearLayout> 
 

 
    <SeekBar 
 
     android:layout_width="fill_parent" 
 
     android:layout_height="wrap_content" 
 
     android:id="@+id/seekBar" 
 
     android:max="255"/> 
 
</LinearLayout>

Ich kann nicht Screenshot zur Zeit veröffentlichen, weil es gesagt

Also, ich möchte den Bildschirm durch 3 Teile vertikal teilen, und teilen Sie den oberen Teil um 2 horizontal und den unteren Teil um 3 horizontal und behalten ihr Verhältnis unabhängig von Bildschirmgröße, Auflösung und Ausrichtung.

Der einzige Teil, der nicht funktionierte, ist der obere Block, auf den Code, den ich es auf android:layout_height="150dp", weil das Layout irgendwie gebrochen ist, wenn ich diese auf android:layout_height="fill_parent" setzen. Aber das macht das Layout nicht proportional, wenn ich die Ausrichtung ändere. Jede Hilfe wird geschätzt.

Vielen Dank.

+0

Sie sollten über 'LinearLayout' &' Gewicht' lesen! –

+0

Wenn Sie 150dp Layout möchten, warum verwenden Sie Gewicht-Konzept für anderes Layout? –

+0

Hallo, vielen Dank für den Hinweis. Ich habe das Problem jetzt gefunden. Ich habe diese auf 'android: layout_height =" fill_parent "' gesetzt, aber vergessen, das Gewicht einzustellen. Entschuldigung, ein Anfängerfehler. – Wellsen

Antwort

2

Durch die Verwendung von layout_weight auf den drei linearLayout und die Zuordnung zu jedem derselben Wert, wird der Bildschirm unabhängig von der Bildschirmgröße vertikal auf 3 geteilt. Außerdem weisen Sie die Höhe zu 0dp: android:layout_height="0dp"

Der komplette xml wäre:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:baselineAligned="false" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" > 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:gravity="left" /> 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:gravity="right" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" > 

     <TextView 
      android:id="@+id/textView3" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" > 

     <TextView 
      android:id="@+id/textView4" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:gravity="left" /> 

     <TextView 
      android:id="@+id/textView5" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:gravity="center" /> 

     <TextView 
      android:id="@+id/textView6" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:gravity="right" /> 
    </LinearLayout> 

    <SeekBar 
     android:id="@+id/seekBar" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:max="255" /> 

</LinearLayout> 
+0

Hallo, vielen Dank, als Antwort markiert. – Wellsen

2

Sie können auch den Bildschirm Dimension pragmatisch und stellen Sie die Größe/Gewicht basierend auf Ihrer Anforderung erhalten.

Display display = getWindowManager().getDefaultDisplay(); 
    DisplayMetrics outMetrics = new DisplayMetrics(); 
    display.getMetrics(outMetrics); 

    float density = getResources().getDisplayMetrics().density; 
    float dpHeight = outMetrics.heightPixels/density; 
    float dpWidth = outMetrics.widthPixels/density; 
4

Es scheint, Leute haben bereits Ihre Frage beantwortet, während ich dies machte. enter image description here Nichtsdestotrotz poste ich diese Antwort für den Fall, dass Sie noch Hilfe brauchen. Dieses Bild enthält die Gewichte, die Sie Ihren Layouts grafisch zuordnen müssen. Entschuldigen Sie einen Mangel an Ausrichtung, ich habe es in Eile gemacht.

7
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:background="#555555"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="horizontal" 
     android:background="#555555"> 
     <TextView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="part1" 
      android:background="#008000"/> 
     <TextView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="part2" 
      android:background="#0000FF"/> 
    </LinearLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="#ffffff"></LinearLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:orientation="horizontal" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="#000000"> 
     <TextView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="part1" 
      android:background="#008000"/> 
     <TextView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="part2" 
      android:background="#A9F114"/> 
     <TextView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:text="part3" 
      android:background="#B4155D"/> 
    </LinearLayout> 
</LinearLayout>