2016-10-08 5 views
0

이 질문에 대한 답변을하지 못 했으므로 다른 인스턴스를 추가하겠습니다. 오리지널은 여기에 있습니다. 정확하게 제 문제입니다. 다음은 XML은 다음과 같습니다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:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.slidamusic.gridlayoutdemo.GridDemo"> 

<GridLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    > 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="fill" 
     android:text="New Button" 
     android:id="@+id/button" 

     android:layout_row="0" 

     android:layout_rowWeight="1" 
     android:layout_columnWeight="1" 
     android:layout_column="0" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="fill" 
     android:text="New Button" 
     android:id="@+id/button1" 

     android:layout_row="0" 

     android:layout_rowWeight="1" 
     android:layout_columnWeight="1" 
     android:layout_column="1" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="fill" 
     android:text="New Button" 
     android:id="@+id/button2" 

     android:layout_row="1" 

     android:layout_rowWeight="1" 
     android:layout_columnWeight="1" 
     android:layout_column="0" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="fill" 
     android:text="New Button" 
     android:id="@+id/button3" 

     android:layout_row="1" 

     android:layout_rowWeight="1" 
     android:layout_columnWeight="1" 
     android:layout_column="1" /> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="fill" 
     android:text="New Button" 
     android:id="@+id/button4" 

     android:layout_row="2" 

     android:layout_rowWeight="1" 
     android:layout_columnWeight="1" 
     android:layout_column="0" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="fill" 
     android:text="New Button" 
     android:id="@+id/button5" 

     android:layout_row="2" 

     android:layout_rowWeight="1" 
     android:layout_columnWeight="1" 
     android:layout_column="1" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="fill" 
     android:text="New Button" 
     android:id="@+id/button6" 

     android:layout_row="3" 

     android:layout_rowWeight="1" 
     android:layout_columnWeight="1" 
     android:layout_column="0" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="fill" 
     android:text="New Button" 
     android:id="@+id/button7" 

     android:layout_row="3" 

     android:layout_rowWeight="1" 
     android:layout_columnWeight="1" 
     android:layout_column="1" /> 



</GridLayout> 

그것은 미리보기에서 모두 완벽하게 정렬하고 동일하게 2 COLS, 4 행 크기의 8 개 버튼과 같은 매우 간단한 레이아웃입니다. 내 태블릿에서 프로그램을 실행하면 상황이 더 많거나 적은 다음과 같이 더 무질서 많이있다 : 나는 문제가 안드로이드와 함께있을 수 있다는 것을 가정 한

GridLayout does not expand elements evenly

: layout_gravity이 = 내가 제거 할 때와 같이 "입력" 이 라인은 버튼이 정상 크기이고 줄이 잘 잡혀 있지만, 원하는대로 화면을 채우지 않으며 미리보기에 표시됩니다.

답변

0

문제점을 발견했습니다. 오래된 API와의 호환성이 부족합니다.

layout_rowWeight 및 layout_columnWeight는 API 21 이상에서만 사용할 수 있습니다. 내 장치는 API 17을 사용합니다.이 문제에 대한 해결책이있을 수 있지만 가장 좋은 아이디어는 단추의 크기를 하드 코딩하는 것과 같은 시각적 결과를 얻기 위해 다른 종류의 서식을 사용하는 것입니다.

나는 온라인 과정을 수강하고 있으며 프로그램을 정확히 작성한 후, 왜 이것이 제대로 작동하지 않는지에 대해 특별히 관심을 보였습니다. Android Studio의 미리보기를 API 23으로 설정했습니다. 테스트 도구를위한 적절한 API로 설정하면 프로그램이 어떻게 보이는지 정확하게 알 수있었습니다. 누군가가 이것을보고 더 낮은 API에서 이러한 속성을 사용하는 방법에 대한 아이디어가 있다면, 어떻게하는지 배우는 것에 관심이있을 것입니다.

Android Studio 자체에 이러한 속성의 사용에 대한 경고가 있으며 마침내 나타났습니다. 엄청난 시간을 절약 할 수있었습니다!