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>
대단원! 안타깝게도 API21에서만 작동합니다 :(이전 버전을 지원하려면 어떻게합니까? – Elye
예, Java API에서는 GridView의 크기를 계산 한 다음 뷰의 크기를 정확하게 설정합니다 (픽셀 단위로).) 대신 무게를 사용;) 도움이 되었다면 답변을 수락하십시오. –
21 세 미만의 더 좋은 해결책을 찾으려면이 페이지를 참조하십시오. http://stackoverflow.com/questions/10016343/gridlayout-not-gridview-how-to-stretch-all -children-evenly –