如何针对LinearLayout设置关于GridView或GridView内容的LinearLayout内容?

时间:2013-09-10 04:32:42

标签: android android-linearlayout android-gridview

我想将LinearLayout内容与gridview对齐。在下面的日历布局中,当天(即Sun,Mon..etc)之间的差距应该等于gridview空间(1,2..30)。应该避免当天的背景图片。

布局代码:

<?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" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2.5"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="horizontal" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:background="@drawable/arrow_left" />
        </LinearLayout>

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1.5"
            android:gravity="center"
            android:orientation="horizontal"
            android:text="hello world"
            android:textColor="#000000"
            android:textSize="18dip"
            android:textStyle="bold" >
        </TextView>

        <LinearLayout
            android:id="@+id/next"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="horizontal" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:background="@drawable/arrow_right" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/previous"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.4"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="horizontal"
            android:text="Sun"
            android:textColor="#000000" >
        </TextView>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="horizontal"
            android:text="Mon"
            android:textColor="#000000" >
        </TextView>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="horizontal"
            android:text="Tue"
            android:textColor="#000000" >
        </TextView>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="horizontal"
            android:text="Wed"
            android:textColor="#000000" >
        </TextView>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="horizontal"
            android:text="Thu"
            android:textColor="#000000" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="horizontal"
            android:text="Fri"
            android:textColor="#000000" >
        </TextView>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="horizontal"
            android:text="Sat"
            android:textColor="#000000" >
        </TextView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="20"
        android:orientation="vertical" >

        <GridView
            android:id="@+id/gridview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center_horizontal"
            android:listSelector="@android:color/black"
            android:numColumns="7"
            android:stretchMode="columnWidth" />
    </LinearLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

1. Add one more property to your GridView is android:gravity="center"
2. GridView item layout should something like below

//  GridView Item layout XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:layout_weight="1" >
     <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="1"
            android:textColor="#000000" >
        </TextView>
    </LinearLayout>