2016-10-08 9 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つのボタンのように非常にシンプルなレイアウトです。私はタブレット上でプログラムを実行すると、物事ははるかに無秩序あり、多かれ少なかれ、このような:layout_gravityは=私が削除したときに「塗りつぶし」:

GridLayout does not expand elements evenly

私はこの問題は、アンドロイドとなり得ると仮定していますこの行はボタンが普通のサイズで、うまく整列していますが、プレビューに表示されているように、画面には表示されません。

答えて

0

私はこの問題を発見しました。それは古いAPIとの互換性の欠如に関するものです。

layout_rowWeightとlayout_columnWeightは、API 21以上でのみ使用できます。私のデバイスはAPI 17を使用していますが、この問題の解決策があるかもしれませんが、ボタンのサイズをハードコーディングするなど、同じ視覚的結果を得るために、他の種類の書式を使用することをお勧めします。

私はオンラインコースを受講しており、プログラムの作成に正確に従っているため、なぜこれが動作していないのかについて特に関心がありました。私はAndroid StudioのプレビューをAPI 23に設定しました。私のテストデバイス用の適切なAPIに設定すると、プログラムがどのように見えるかが正確にわかりました。もし誰かがこれを見て、より低いAPIでこれらの属性を使う方法について考えているなら、それをやる方法を学ぶことに興味があります。

Android Studio自体にこれらの属性の使用に関する警告があり、最後に気付きました。膨大な時間を節約できました!

関連する問題