2016-07-31 22 views
0

ich ziemlich neu bin, zu Android und xml Layouten. Ich habe versucht, eine App zu machen, und schon mit dem Programmier-Bit fertig. Ich stehe jedoch fest auf die Finalisierung des Layouts. Brauche wirklich eine Hand. Meine app soll wie folgt aussehen:Make Linear-Layout nicht den Bildschirm verlassen

Layout

ich den Bildschirm in 4 Linear Layouts geteilt habe, als ich in dem Bild nummeriert habe, wie unten gesehen:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
tools:context="com.FirstVersion.android.cwd.Play" 
android:id="@+id/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:background="@color/material_blue_grey_800"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/btnGoBack" 
     android:text="Go Back" 
     android:layout_weight="1" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Profile Information" 
     android:background="@color/accent_material_dark" 
     android:layout_weight="10" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/LDrawerAction" 
     android:text="Menu" 
     android:clickable="false" 
     android:layout_weight="1" 
     android:layout_marginRight="5dp" 
     android:layout_marginLeft="5dp" /> 

</LinearLayout> 

//endregion 

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:id="@+id/Section2" 
android:orientation="vertical"> 

</LinearLayout> 

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:id="@+id/Section3" 
android:orientation="horizontal"> 

    <TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textSize="5sp" 
    android:textColor="@color/colorWhite" 
    android:background="#990000" 
    android:maxLines = "3" 
    android:scrollbars = "vertical" 
    android:id="@+id/txtSection3" /> 

</LinearLayout> 

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:weightSum="15" 
android:orientation="vertical" 
android:baselineAligned="false"> 

    <TableLayout 
    android:id="@+id/Section4" 
    android:layout_width="match_parent" 
    android:layout_weight="15" 
    android:layout_height="wrap_content"> 

    </TableLayout> 
</LinearLayout> 

</LinearLayout> 
</android.support.v4.widget.DrawerLayout> 

Das Problem ist jetzt, dass, Der zweite Abschnitt ist programmgesteuert gefüllt und hat einen Zeilenbereich von 8 bis 14. Dies gilt auch für die Spalten. Jeder Block ist eine andere Taste, die mich mit diesem Code hinzufügen:

Lay= (LinearLayout)findViewById(R.id.Section2); 
for (int row = 0; row < numberOfRows; row++) 
    { 
     LinearLayout Linearrow = new LinearLayout(this); 
     Linearrow.setOrientation(LinearLayout.HORIZONTAL); 

     for (int column = 0; column < numberOfColumns; column++) 
     { 
      View a = Chosen.blocks[row][column]; //This is a View the extend Button that has no layout properties defined 

      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
        LayoutParams.WRAP_CONTENT, 1.0f); 

      a.setLayoutParams(params); 

      LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); 
      lp.weight = 1.0f; 
      Linearrow.addView(a, lp); 
     } 
     LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); 
     lp.weight = 1.0f; 
     Lay.addView(Linearrow, lp); 
    } 

ich versucht habe sogar ein Tablelayout verwenden und ein kleines Bild als Hintergrund zuweisen, so dass es mit dem folgenden Code der Größe verändert bekommen konnte:

TL =(TableLayout)fa.findViewById(R.id.Section2); 

    for (int i = 0; i < numberOfRows; i++) 
    { 
     TableRow tableRow = new TableRow(c); 

     for(int j = 0; j < numberOfColumns; j++) 
     { 
      String s = T[(i * 10) + j]; 
      Block b = new Block(c, s); 
      //int w = Math.round(c.getResources().getDisplayMetrics().density * 20); 
      //int h = Math.round(c.getResources().getDisplayMetrics().density * 80); 
      //b.setLayoutParams(new TableRow.LayoutParams(w, h, 2.0f)); 

      b.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f)); 
      b.setBackgroundResource(R.drawable.g_3); 
      tableRow.addView(b); 
      tableRow.setLayoutParams(new android.widget.LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 5f)); 

      final Block fb = b; 
     } 
     TL.addView(tableRow); 
    } 

wenn ich die app laufen, kann ich sehen, dass, wenn ich es auf einem großen Bildschirm Telefon öffnen, wird der Inhalt nicht sichtbar ist ganz (als ob die Höhe manuell einstellen der Layouts festgelegt ist), und da es nicht Scroll-fähig ist, Der Inhalt ist nicht sichtbar. Wie kann ich das Layout neu anordnen oder ändern, so dass es wie im Bild oben aussieht?

+0

Für die Spalten der Tabelle Layout, können Sie die Stretch-Spalten finden Attribut nützlich –

+0

Warum nicht Sie 'android.support.v7.widget.RecyclerView' für diesen dynamischen Inhalt? In einem anderen Fall fügen Sie ein Layout, das blättert nämlich 'android.support.v4.widget.NestedScrollView' als Elternteil' LinearLaout' mit Höhe und Breite wie 'match_parent'. – JavaGhost

+0

@ cricket_007 Ich habe es bereits als eine Eigenschaft festgelegt und macht keinen Unterschied. – blu3f1rest0rm

Antwort

0

Fabrikat festen Höhe von Layouts Siehe layout_weight

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.1" 
     android:weightSum="12" 
     android:orientation="horizontal" 
     android:background="@color/material_blue_grey_800"> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/btnGoBack" 
      android:text="Go Back" 
      android:layout_weight="1" 
      android:layout_marginLeft="5dp" 
      android:layout_marginRight="5dp" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Profile Information" 
      android:background="@color/accent_material_dark" 
      android:layout_weight="10" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/LDrawerAction" 
      android:text="Menu" 
      android:clickable="false" 
      android:layout_weight="1" 
      android:layout_marginRight="5dp" 
      android:layout_marginLeft="5dp" /> 

    </LinearLayout> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.4" 
     android:weightSum="1" 
     android:id="@+id/Section2" 
     android:orientation="vertical"> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.1" 
     android:id="@+id/Section3" 
     android:orientation="horizontal"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textSize="5sp" 
      android:textColor="@android:color/white" 
      android:background="#990000" 
      android:maxLines = "3" 
      android:scrollbars = "vertical" 
      android:id="@+id/txtSection3" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.4" 
     android:weightSum="15" 
     android:orientation="vertical" 
     android:baselineAligned="false"> 

     <TableLayout 
      android:id="@+id/Section4" 
      android:layout_width="match_parent" 
      android:layout_weight="15" 
      android:layout_height="wrap_content"> 

     </TableLayout> 
    </LinearLayout> 

</LinearLayout> 
</android.support.v4.widget.DrawerLayout> 

Und in Ihrer Java-Klasse mit dem Sie Ansichten dynamisch hinzufügen, berechnen die Gewichte von Kindern entsprechend gegeben, wie unten

Lay= (LinearLayout)findViewById(R.id.Section2); 
Lay.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); 
int totalWidth = Lay.getMeasuredWidth(); 
float rowWeight = 1/numberOfRows; 
int columnWidth = totalWidth/numberOfColumns; 
for (int row = 0; row < numberOfRows; row++) 
{ 
    LinearLayout Linearrow = new LinearLayout(this); 
    Linearrow.setOrientation(LinearLayout.HORIZONTAL); 

    for (int column = 0; column < numberOfColumns; column++) 
    { 
     View a = Chosen.blocks[row][column]; //This is a View the extend Button that has no layout properties defined 
     // height is determined by row's layout weight 
     LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(columnWidth, 
       LinearLayout.LayoutParams.MATCH_PARENT, 1.0f); 

     a.setLayoutParams(params); 
     Linearrow.addView(a); 
    } 
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); 
    lp.weight = rowWeight; 
    Lay.addView(Linearrow, lp); 
} 

Ich habe diese nicht getestet, aber könnte nur mit einigen Änderungen arbeiten, die Sie herausfinden können, sobald Sie diesen Code ausführen.

+0

Ich habe hier die Lösung ausprobiert und sogar die von dir angegebene Methode in die Kommentare der Frage eingefügt. Was ich falsch gemacht habe, war die Höhe der Sektionen, da man das Gewicht brauchte, um alle die Höhe von 0d zu erreichen, und nicht nur einige. Danke – blu3f1rest0rm

0

Verwenden

android:layout_weight 

Attribut in gleichen Teilen in linearer Layout anzuordnen. im Link

+1

Ich habe bereits das Gewicht der Elemente innerhalb der linearen festgelegt. Das Problem liegt im linearenLayout selbst, da es den Bildschirm erweitert, wenn die Elemente hinzugefügt werden. Ich habe sogar die Gewichtssumme hinzugefügt. – blu3f1rest0rm

+0

Fügen Sie linearen Layouts Gewicht hinzu, sodass sie basierend auf der Bildschirmgröße feste Höhen haben. Wenn Sie dann untergeordnete Elemente hinzufügen, passen sie sich an. – JavaGhost

+0

@JavaGhost Wird es beim dynamischen Laden von Ansichten auf den Bildschirm passen? –

0

Es ist besser, Blick in ScrollView zu verwenden. Verwenden Sie den folgenden Code: -

<?xml version="1.0" encoding="utf-8"?> 
    <android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context="com.FirstVersion.android.cwd.Play" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:background="@color/material_blue_grey_800"> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/btnGoBack" 
      android:text="Go Back" 
      android:layout_weight="1" 
      android:layout_marginLeft="5dp" 
      android:layout_marginRight="5dp" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Profile Information" 
      android:background="@color/accent_material_dark" 
      android:layout_weight="10" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/LDrawerAction" 
      android:text="Menu" 
      android:clickable="false" 
      android:layout_weight="1" 
      android:layout_marginRight="5dp" 
      android:layout_marginLeft="5dp" /> 

    </LinearLayout> 

    //endregion 
    <ScrollView 
android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/Section2" 
    android:orientation="vertical"> 

    </LinearLayout> 

    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/Section3" 
    android:orientation="horizontal"> 

     <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textSize="5sp" 
     android:textColor="@color/colorWhite" 
     android:background="#990000" 
     android:maxLines = "3" 
     android:scrollbars = "vertical" 
     android:id="@+id/txtSection3" /> 

    </LinearLayout> 

    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:weightSum="15" 
    android:orientation="vertical" 
    android:baselineAligned="false"> 

     <TableLayout 
     android:id="@+id/Section4" 
     android:layout_width="match_parent" 
     android:layout_weight="15" 
     android:layout_height="wrap_content"> 

     </TableLayout> 
    </LinearLayout> 
    </LinearLayout> 
</ScrollView> 
    </LinearLayout> 
    </android.support.v4.widget.DrawerLayout> 
+0

Wie ich in der Frage angegeben habe, verwende ich keinen scrollview und ich kann nicht, da ich alle Informationen zeigen muss – blu3f1rest0rm