2013-09-02 20 views
35

Mam 2 kolumny w mojej GridLayout. Chcę, aby te kolumny zajmowały połowę szerokości ekranu, a następnie ich zawartość podrzędna wypełniała szerokość/wysokość własnych komórek. Próbowałem ustawić dzieci na fill_parent, ale to właśnie powoduje, że pierwszy przejmie cały układ. I wydaje się, że GridLayout nie obsługuje weight? Może jest lepszy układ do wykorzystania, ale chcę układ w stylu Grid, więc wydaje się, że jest to naturalny wybór.GridLayout Szerokość kolumny

+1

Czy próbujesz wypełnić określoną wysokość/szerokość? Czy chcesz, aby wszystkie elementy siatki miały taką samą wysokość? –

+2

tak. Mam 2 kolumny. Chcę, aby ich szerokość wynosiła 50% szerokości ekranu, więc razem będą równe szerokości ekranu. a następnie ich wysokość jest taka sama jak szerokość w pikselach. Więc będą kwadratowe. – bwoogie

+0

znalazłeś jakieś rozwiązanie? jeśli tak, proszę udostępnij. Też chcę tego samego rodzaju funkcjonalności. – BSKANIA

Odpowiedz

5

OK, więc zrezygnowałem z widoku siatki i wykorzystałem tylko układy liniowe. Zrobiłem pionową, a następnie dodałem 2 poziomy. Jest nieco bardziej zaangażowany niż widok siatki ... ale dopóki nie zorientuję się, że to działa.

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:orientation="vertical" > 

     <ImageButton 
      android:id="@+id/btn_mybutton" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="5dp" 
      android:background="@color/pomegranate" 
      android:contentDescription="@string/contentDescriptionmybutton" 
      android:src="@drawable/ic_launcher" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:orientation="vertical" > 

     <ImageButton 
      android:id="@+id/btn_prefs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="5dp" 
      android:background="@color/pomegranate" 
      android:contentDescription="@string/contentDescriptionSettings" 
      android:src="@drawable/ic_settings" /> 

    </LinearLayout> 

</LinearLayout> 

a potem dodać do tego przyciski kwadratowe :)

@Override 
public void onWindowFocusChanged(boolean hasFocus) { 
    super.onWindowFocusChanged(hasFocus); 

    btnPrefs.setMinimumHeight(btnPrefs.getWidth()); 
    btnVerse.setMinimumHeight(btnMyButton.getWidth()); 

} 
+0

Chciałem, aby moje przyciski były tak wysokie, jak długo użyłem kodu, który podałeś. To działa, ale zastanawiam się, dlaczego nie ma ono wpływu na 'onCreate' w' Activity'. Jakieś pomysły? – MikkoP

0

Można rozszerzyć klasę RelativeLayout (lub użyć LinearLayout jeśli chcesz) aby upewnić się, że wysokość przedmiotu będzie taka sama jak wysokość.

public class GridItem extends RelativeLayout { 

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

     public GridItem(Context context, AttributeSet attr) { 
       super(context, attr); 
     } 

     public GridItem(Context context, AttributeSet attr, int integer) { 
       super(context, attr, integer); 
     } 

     // Override onMeasure to give the view the same height as the specified width 
     @Override 
     public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
      super.onMeasure(widthMeasureSpec, widthMeasureSpec); 
      setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); 
     } 

} 

Nadrzędnym widokiem układu przedmiotu powinien być widok GridItem, aby upewnić się, że działa. Musi to być plik układ będziesz nadmuchać w getView Twojego ListAdapter

<?xml version="1.0" encoding="utf-8"?> 
<my.packagename.GridItem xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <!-- The content of the item --> 

</my.packagename.GridItem> 

i ustawić stretchMode z GridView do columnWidth. Spowoduje to dopasowanie wszystkich elementów do szerokości określonej liczby kolumn. Nowy widok sprawi, że będą one miały taką samą wysokość.

<GridView 
    android:id="@+id/gridList" 
    android:numColumns="2" 
    android:stretchMode="columnWidth" 
/> 
32

Ten kod jest dostępny na pre API21 z biblioteką wsparcia!

mam prosty kawałek kodu, aby pokazać 4 przyciski w GridLayout 2 kolumny, które mają 50% dostępnej powierzchni: być może to pomoże

<GridLayout 
    android:id="@+id/grid" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:columnCount="2" 
    > 


    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" 
     android:layout_gravity="fill" 
     android:layout_columnWeight="1" 
     /> 

     <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" 
     android:layout_gravity="fill" 
     android:layout_columnWeight="1" 
     /> 

     <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" 
     android:layout_gravity="fill" 
     android:layout_columnWeight="1" 
     /> 

     <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" 
     android:layout_gravity="fill" 
     android:layout_columnWeight="1" 
     /> 



</GridLayout> 

rozwiązanie jest chyba to:

android:layout_gravity="fill" 
android:layout_columnWeight="1" 
+13

I już dostępny jest jeden pre API 21 z najnowszą wersją biblioteki wsparcia. Wystarczy dodać zależność - com.android.support:gridlayout-v7:22.0.0 – oznus

+1

@oznus Próbowałem z tym, nadal bezużyteczne. Nie przyjmuje właściwości wagi. –

+0

Używając nowego 'GridLayout' powinieneś użyć przestrzeni nazw' app', więc byłoby to 'app: layout_gravity =" fill "' –

7

Dla wstępnego API 21, biblioteka wsparcie zastosowanie:

dodać

compile 'com.android.support:appcompat-v7:23.1.1' 
compile 'com.android.support:design:23.1.1' 

do swoich zależności.

Następnie w pliku XML:

<android.support.v7.widget.GridLayout 
        xmlns:app="http://schemas.android.com/apk/res-auto" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        app:columnCount="2" 
        app:orientation="horizontal" 
        app:rowCount="1"> 

        <TextView 
         android:text="1" 
         android:textStyle="bold" 
         app:layout_columnWeight="1" 
         /> 

        <TextView 
         android:text="2" 
         android:textStyle="bold" 
         app:layout_columnWeight="1" /> 

</android.support.v7.widget.GridLayout> 

Tutaj należy zwrócić uwagę na stosowanie "APP" prefiksu i nie zapomnij dodać

xmlns:app="http://schemas.android.com/apk/res-auto" 

do pliku xml

0

podczas korzystania z Możesz używać setSpanSizeLookup.Oto urywek z mojego projektu, który powinien przyczynić się do korzystania z tej metody prawidłowo:

if (mAdapter == null) { 
    final int columnCount = getResources().getInteger(R.integer.numberGridColumns); 
    mLayoutManager = new GridLayoutManager(getActivity(), columnCount); 
    mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { 
     @Override 
     public int getSpanSize(int position) { 
      switch (mAdapter.getItemViewType(position)) { 
       case ListAdapter.VIEW_TYPE_ONE_COLUMN: 
        return columnCount; 
       case RecipeListAdapter.VIEW_TYPE_FULL_COLUMN: 
       default: 
        return 1; 
      } 
     } 
    }); 
    mRecyclerView.setLayoutManager(mLayoutManager); 

    mAdapter = new RecipeListAdapter(mPresenter); 
    mRecyclerView.setAdapter(mAdapter); 
} 
mAdapter.notifyDataSetChanged(); 
1

dodając widoki dynamicznie w siatce układ 2 kolumny, które mają 50% dostępnej powierzchni:

GridLayout gridLayout = new GridLayout(); 

View view; //it can be any view 

GridLayout.LayoutParams param = new GridLayout.LayoutParams(); 

param.columnSpec = GridLayout.spec(GridLayout.UNDEFINED,GridLayout.FILL,1f); 

param.width = 0; 

view.setLayoutParams(param); 

gridLayout.add(view); 

w sposób statyczny (w pliku .xml).

<android.support.v7.widget.GridLayout 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:alignmentMode="alignBounds" 
    app:columnCount="2" 
    app:columnOrderPreserved="false" 
    app:orientation="horizontal" 
    android:layout_margin="20dp" 
    android:layout_marginBottom="30dp" 
    android:padding="4dp" 
    app:rowCount="2"> 

<TextView 
    android:layout_marginTop="2dp" 
    android:id="@+id/edit_profile_username" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    app:layout_column="0" 
    app:layout_row="0" 
    app:layout_gravity="fill" 
    app:layout_columnWeight="1" 
    android:text="@string/user_name" /> 

<TextView 
    android:layout_marginTop="2dp" 
    android:id="@+id/edit_profile_first_name" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    app:layout_column="1" 
    app:layout_row="0" 
    app:layout_gravity="fill" 
    app:layout_columnWeight="1" 
    android:text="@string/first_name" /> 

<TextView 
    android:layout_marginTop="2dp" 
    android:id="@+id/edit_profile_middle_name" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    app:layout_column="0" 
    app:layout_gravity="fill" 
    app:layout_columnWeight="1" 
    app:layout_row="1" 
    android:text="@string/middle_name" /> 

<TextView 
    android:layout_marginTop="2dp" 
    android:id="@+id/edit_profile_last_name" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    app:layout_column="1" 
    app:layout_gravity="fill" 
    app:layout_columnWeight="1" 
    app:layout_row="1" 
    android:text="@string/last_name" /> 

</android.support.v7.widget.GridLayout> 
Powiązane problemy