10

私は試してみましたが、そのような運はありません。ここでの他のすべての回答は、センタリングGridViewに関連して運が良かっただけでなく、運もありませんでした。これは私のGridViewxmlです:

<GridView
        android:id="@+id/calendar_grid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="7"
        android:columnWidth="40dp"
        android:verticalSpacing="1dp"
        android:horizontalSpacing="1dp"
        android:paddingTop="2dp"
        android:stretchMode="columnWidth"
        android:layout_gravity="right"
        android:gravity="right"
        android:background="#696969" ></GridView>

考えられるすべての組み合わせを試しましたが、うまくいきませんでした。少なくともデバイス間で一貫性がありません。いつもやっているイメージです。

ここに画像の説明を入力

GridView の右側でわかるように、間隔があります。私は常にそれを変えようとしていますが、ほとんどうまくいきません。を使用android:stretchModeすると変更されますが、セル間の間隔が台無しになるため、機能しません。ご覧のとおり、重力の使用も失敗します。他のすべての値を試しましたが、それでも何も変わりません。左にくっついています。また、40 未満のものからサイズを変更しようとしてandroid:columnWidthも何も起こらないのも奇妙です。また、40 を 45 に増やしても、何も変わりません。しかし、50 に変更すると、右側のスペースがなくなります。でも、スマホに入れると隙間が!他の作業を続けるために2週間前にこれをスキップしましたが、まだこの問題を解決していないようです。

誰かがここで私を助けるのに役立つ場合は、セルを作成するための xml を提供することもできます。しかし、それが重要かどうかはわかりません。しかし、ここにあります:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView 
        android:id="@+id/day_num"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="10dp"
        android:background="#000000"
        android:height="35dp"
        android:gravity="center_vertical|center_horizontal" />


</LinearLayout>

それが役立つ場合、GridViewの親はLinearLayout次のとおりです。

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_weight="0"
    android:gravity="center" >

重みが 1 の兄弟もありますが、重みが 0 の場合、その部分が最初にスペースを設定するため、影響があるとは思いません。

編集

特に、この問題を抱えている人はそれほど多くないように見えるので、私はそれをいじり続けましたが、 をどれだけ小さくしてもcolumnWidth、GridView はほとんど小さくなりません。実際、50 dp になるまでは、同じギャップが維持されます。しかし、ポートレートモードにするとギャップが再発するので、90dpまで上げます(その下で試しましたが、何も変更しませんでした)。奇妙なことはGridView、ギャップを埋める以外に変化がないことです。だから私はそれを200dpにすることができ、それは私の画面の大きさや他の小さな画面で完璧に見えるでしょう. タブレットに行くと、もっと増やす必要があると思いますが、要点は、これは奇妙な動作です. 誰かがこれに遭遇しましたか?

4

6 に答える 6

9

GridView内部に aを入れてから、のプロパティを にRelativeLayout設定したい場合があります。このようなもの:GridViewlayout_centerInParenttrue

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

    <GridView
            android:id="@+id/calendar_grid"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:numColumns="7"
            android:columnWidth="40dp"
            android:verticalSpacing="1dp"
            android:horizontalSpacing="1dp"
            android:paddingTop="2dp"
            android:stretchMode="columnWidth"
            android:background="#696969"
            android:layout_centerInParent="true" />

</RelativeLayout>

RelativeLayout子の位置をより適切に制御できます。

于 2012-07-19T07:34:29.523 に答える
7

ここに私の作業構成があります:

        <GridView
        android:id="@+id/gridView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:cacheColorHint="@color/white"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:numColumns="3"
        android:padding="5dp"
        android:scrollbars="none"
        android:stretchMode="columnWidth"
        android:gravity="center"
        />
于 2013-03-06T11:23:21.767 に答える
3

TextViewの親レイアウトの重力またはTextViewのLayoutGravityを設定する必要があります。Plzはセルレイアウトを次のように変更します。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:gravity="center_vertical|center_horizontal" >
    <TextView 
        android:id="@+id/day_num"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="10dp"
        android:background="#000000"
        android:height="35dp"
        />


</LinearLayout>
于 2012-07-19T04:41:22.277 に答える
1

私は同じ問題に遭遇し、centerInParent を使用した RelativeLayout も役に立ちませんでした。

基本的には画面幅に合わせて割り切れる余りが含まれる場合があります。

そのため、GridView を中央に配置する唯一の方法は、残りがなく収まる幅を計算し、残りの値をパディングとして使用することでした。

WindowManager manager = window.getWindowManager();
int  screenWidth      = manager.getDefaultDisplay().getWidth();
int contentWidth      = screenWidth;

while( 0 != contentWidth%COLUMN_NUMBER ) contentWidth--;

columnWidth = contentWidth / COLUMN_NUMBER;
int padding = screenWidth - contentWidth;

次に、GridView を作成するときに:

gridView.setColumnWidth(columnWidth);
gridView.setPadding(padding / 2, 0, padding / 2, 0);
于 2013-01-10T02:38:20.927 に答える