2016-10-09 3 views
0

2x2의 간단한 그리드 레이 아웃이 있습니다. 그들 중 3 명은 빨강, 초록 및 파랑을 볼 수 있습니다. 그리드의 셀에 가운데 정렬되도록합니다. 내 레이아웃은 아래와 같지만 모든 뷰에 대해 layout_gravity를 중심으로 배치 한 경우에도 뷰가 왼쪽 상단에 정렬됩니다. 어떻게 해결할 수 있을까요?GridLayout 센터의 그리드 셀에서 뷰를 정렬하는 방법은 무엇입니까?

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.elyeproj.griddrag.MainActivity"> 

    <GridLayout 
     android:id="@+id/container_grid" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:columnCount="2" 
     android:rowCount="2" 
     android:orientation="horizontal"> 
     <View 
      android:id="@+id/view_red" 
      android:layout_height="100dp" 
      android:layout_width="100dp" 
      android:layout_gravity="center" 
      android:background="#ff0000" /> 

     <View 
      android:id="@+id/view_green" 
      android:layout_height="100dp" 
      android:layout_width="100dp" 
      android:layout_gravity="center" 
      android:background="#00ff00" /> 

     <View 
      android:id="@+id/view_blue" 
      android:layout_height="100dp" 
      android:layout_width="100dp" 
      android:layout_gravity="center" 
      android:background="#0000ff" /> 
    </GridLayout> 
</RelativeLayout> 

답변

1

그리드 레이아웃 항목이 줄 바꿈되어 원하는대로 제대로 정렬 할 수 있습니다.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_main" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
> 

<GridLayout 
    android:id="@+id/container_grid" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:columnCount="2" 
    android:rowCount="3" 
    android:orientation="horizontal"> 

    <View 
     android:id="@+id/view_red" 
     android:layout_height="100dp" 
     android:layout_width="100dp" 
     android:layout_columnWeight="1" 
     android:layout_rowWeight="1" 
     android:layout_gravity="center" 
     android:background="#ff0000" 
     android:layout_row="0" 
     android:layout_column="0" /> 

    <View 
     android:id="@+id/view_green" 
     android:layout_height="100dp" 
     android:layout_width="100dp" 
     android:layout_columnWeight="1" 
     android:layout_gravity="center" 
     android:background="#00ff00" 
     android:layout_row="0" 
     android:layout_column="1" /> 

    <View 
     android:id="@+id/view_blue" 
     android:layout_height="100dp" 
     android:layout_width="100dp" 
     android:layout_columnWeight="1" 
     android:layout_rowWeight="1" 
     android:layout_gravity="center" 
     android:background="#0000ff" 
     android:layout_row="1" 
     android:layout_column="0" /> 
    </GridLayout> 
</RelativeLayout> 

21 이하 API에 대한 LinearLayout을 사용하여 다른 솔루션을 아래 링크를 참조하십시오 : 는 GridLayout (not GridView) how to stretch all children evenly

+0

대단원! 안타깝게도 API21에서만 작동합니다 :(이전 버전을 지원하려면 어떻게합니까? – Elye

+0

예, Java API에서는 GridView의 크기를 계산 한 다음 뷰의 크기를 정확하게 설정합니다 (픽셀 단위로).) 대신 무게를 사용;) 도움이 되었다면 답변을 수락하십시오. –

+0

21 세 미만의 더 좋은 해결책을 찾으려면이 페이지를 참조하십시오. http://stackoverflow.com/questions/10016343/gridlayout-not-gridview-how-to-stretch-all -children-evenly –

1

헤이 단지 각보기에이 줄을 추가 XML 아래 당신이 원하는합니다.

android:layout_columnWeight="1" 
android:layout_rowWeight="1" 

희망이 있습니다.

+0

좋아요! 불행히도 그것은 API21에서만 작동합니다 : (이전 버전을 지원하기를 원한다면? – Elye

+0

API19 장치에서 이것을 실행하려고 시도했는지 확인하십시오. 작동하는지 확인하십시오. – Harv

관련 문제