0

我在 XML 中有一个复杂的布局。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/mainbackground"
android:orientation="vertical"
android:weightSum="1" >

<LinearLayout
    android:id="@+id/selector"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="0.1"
    android:orientation="horizontal"
    android:weightSum="1" >

    <TextView
        style="@style/T2"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:gravity="center"
        android:text="@string/month" />

    <TextView
        android:id="@+id/byday"
        style="@style/T2"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:background="@color/tertiarybackground"
        android:gravity="center"
        android:text="@string/day"
        android:textColor="@color/links" />
</LinearLayout>

<RelativeLayout
    android:id="@+id/header"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_below="@+id/selector"
    android:layout_weight="0.1"
    android:background="@color/mainbackground" >

    <RelativeLayout
        android:id="@+id/previous"
        android:layout_width="48dip"
        android:layout_height="48dip"
        android:layout_alignParentLeft="true" >

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

    <TextView
        android:id="@+id/title"
        style="@style/H2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dip"
        android:textColor="#000000" />

    <RelativeLayout
        android:id="@+id/next"
        android:layout_width="48dip"
        android:layout_height="48dip"
        android:layout_alignParentRight="true" >

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

<LinearLayout
    android:id="@+id/daysoftheweek"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_below="@+id/header"
    android:layout_weight="0.05"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" >

        <TextView
            style="@style/T2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/sun" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" >

        <TextView
            style="@style/T2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/mon" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" >

        <TextView
            style="@style/T2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tue" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" >

        <TextView
            style="@style/T2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/wed" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" >

        <TextView
            style="@style/T2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/thu" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" >

        <TextView
            style="@style/T2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/fri" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" >

        <TextView
            style="@style/T2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/sat" />
    </LinearLayout>
</LinearLayout>

<GridView
    android:id="@+id/gridview"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_below="@+id/daysoftheweek"
    android:layout_gravity="center_horizontal"
    android:layout_weight="0.5"
    android:listSelector="@android:color/transparent"
    android:numColumns="7"
    android:stretchMode="columnWidth" />

<ScrollView
    android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.25"
    android:fadeScrollbars="false"
    android:gravity="top"
    android:scrollbarAlwaysDrawHorizontalTrack="true"
    android:scrollbarAlwaysDrawVerticalTrack="true"
    android:scrollbarFadeDuration="0" >

    <LinearLayout
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_gravity="top"
        android:orientation="vertical" >
    </LinearLayout>
</ScrollView>

</LinearLayout>

现在我以编程方式将行添加到 LinearLayout text

rLayout = (LinearLayout) findViewById(R.id.text);
View row = li.inflate(R.layout.event_row, null);
rLayout.addView(row);

这是我得到的结果。

如您所见,该行放置在中心,而不是textLinearLayout的顶部

在此处输入图像描述

谢谢你的帮助!

4

2 回答 2

1

对 LinearLayout 使用 layout_gravity="top" 属性。

<LinearLayout
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_gravity="top"
        android:orientation="vertical" >
</LinearLayout>
于 2013-10-19T17:30:49.190 回答
0

解决了!

问题出在上面的 GridView 中:

我改变了形式:

<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_below="@+id/daysoftheweek"
android:layout_gravity="center_horizontal"
android:layout_weight="0.5"
android:listSelector="@android:color/transparent"
android:numColumns="7"
android:stretchMode="columnWidth" />

<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/daysoftheweek"
android:layout_gravity="center_horizontal"
android:listSelector="@android:color/transparent"
android:numColumns="7"
android:stretchMode="columnWidth" />
于 2013-10-19T18:02:22.330 回答