2017-06-27 2 views
1

Ich muss 6 Quadrate horizontal über die Breite des Bildschirms platzieren. Ich weiß, dass ich dies zu erreichen, wie folgt:Android: Platzieren Sie 6 Quadrate horizontal mit XML

  <LinearLayout 
       android:weightSum="6" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 

       <View 
        android:id="@+id/firstSquare" 
        android:layout_width="0dp" 
        android:layout_weight="1" 
        android:layout_height="60dp"/> 

       <View 
        android:id="@+id/secondSquare" 
        android:layout_width="0dp" 
        android:layout_weight="1" 
        android:layout_height="60dp"/> 

          . 
          . 
          . 

      </LinearLayout> 

ich sicherstellen möchten, dass die Höhe jedes View in LinearLayout seiner Breite gleich ist.

Gibt es eine Möglichkeit, sie direkt über XML ohne Grid erreichen können, da Gewichte von API 21. Meine Mindest SDK unterstützt wird API 16. Ich weiß, dass ich dies programmatisch, indem man die Breite jedes View Ziel erreichen kann und dann seine Höhe gleich der Breite einstellen, aber ich möchte es vermeiden.

+0

Etwas Ähnliches wurde hier gefragt: https://stackoverflow.com/a/32291319/5408578 –

+0

Mögliches Duplikat [Gridlayout (nicht Gridview), wie alle Kinder strecken gleichmäßig] (https : //stackoverflow.com/questions/10016343/gridlayout-not-gridview-how-to-stretch-all-children-evenly) –

+0

ok, so dass Sie nicht Fix Höhe? Recht? – Vij

Antwort

1

hier ist Java-Code für CustomView.java

public class Customlinearlayout {

public CustomView(Context context) { 
    super(context); 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    int parentWidth = MeasureSpec.getSize(widthMeasureSpec); 
    int parentHeight = MeasureSpec.getSize(heightMeasureSpec); 
    this.setMeasuredDimension(parentWidth, parentWidth); 
    super.onMeasure(widthMeasureSpec, widthMeasureSpec); 
} 

}

und sample_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:weightSum="6"> 

<CustomView 
    android:id="@+id/firstSquare" 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:background="@color/colorPrimary" 
    android:layout_height="wrap_content"/> 


<CustomView 
    android:id="@+id/secondSquare" 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:background="@color/colorPrimary" 
    android:layout_height="wrap_content"/> 

<CustomView 
    android:id="@+id/thirdSquare" 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:background="@color/colorPrimary" 
    android:layout_height="wrap_content"/> 

<CustomView 
    android:id="@+id/fourthSquare" 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:background="@color/colorPrimary" 
    android:layout_height="wrap_content"/> 

<CustomView 
    android:id="@+id/fifthSquare" 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:background="@color/colorPrimary" 
    android:layout_height="wrap_content"/> 
<CustomView 
    android:id="@+id/sixthSquare" 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:background="@color/colorPrimary" 
    android:layout_height="wrap_content"/> 

erstreckt

hilfreich sein kann ..

-1
<LinearLayout 
    android:layout_gravity="center_horizontal" 
    android:orientation="horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="50dp"> 

    <View 
     android:layout_width="50dp" 
     android:layout_height="match_parent"/> 

    <View 
     android:layout_width="50dp" 
     android:layout_height="match_parent"/> 
    <View 
     android:layout_width="50dp" 
     android:layout_height="match_parent"/> 

    <View 
     android:layout_width="50dp" 
     android:layout_height="match_parent"/> 

    <View 
     android:layout_width="50dp" 
     android:layout_height="match_parent"/> 

    <View 
     android:layout_width="50dp" 
     android:layout_height="match_parent"/> 
</LinearLayout> 
Verwandte Themen