将片段放置在屏幕顶部的线性布局中

时间:2015-02-24 12:06:03

标签: android android-layout

我使用LinearLayout将屏幕拆分为两部分,一部分包含一个片段,另一部分包含一个卷轴内的两个片段。 我正在使用android studio,在预览模式下,屏幕显示两个站点的片段都放在顶部,这就是我想要的。但是,当我尝试使用设备时,这些片段会放在屏幕中间。但是我希望顶部有两列。 我试图在LinearLayout容器上使用layout_gravitity和gravitity,但它不起作用。

这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top"
android:gravity="top">

<fragment
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="org.gvsig.roads.mobile.fragments.itineraries.NewItineraryTabReferenceFrag_"
    android:id="@+id/frag_reference_new_itinerary"
    tools:layout="@layout/new_itinerary_tab_reference_frag"
    android:layout_weight="1"
    android:gravity="top" />


<ScrollView
    android:layout_marginLeft="@dimen/between_contents_margin"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1">

    <RelativeLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <fragment
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:name="org.gvsig.roads.mobile.fragments.itineraries.NewItineraryTabTimesFrag_"
            android:id="@+id/frag_time_new_itinerary"
            tools:layout="@layout/new_itinerary_tab_times_frag" />
        <fragment
                android:layout_marginTop="@dimen/between_contents_margin"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:name="org.gvsig.roads.mobile.fragments.itineraries.NewItineraryTabNotesFrag_"
                android:id="@+id/frag_notes_new_itinerary"
                android:layout_below="@+id/frag_time_new_itinerary"

                tools:layout="@layout/new_itinerary_tab_notes_frag"
            android:layout_alignParentEnd="false"
            android:layout_alignParentBottom="false" />


    </RelativeLayout>
</ScrollView>
</LinearLayout>

2 个答案:

答案 0 :(得分:0)

试试这个。请注意widths = 0dp 。这是衡量权重的必要条件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="top"
    android:gravity="top"
    >
    <!-- This will  be 100% high, 50 wide -->
    <fragment
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:name="org.gvsig.roads.mobile.fragments.itineraries.NewItineraryTabReferenceFrag_"
        android:id="@+id/frag_reference_new_itinerary"
        tools:layout="@layout/new_itinerary_tab_reference_frag"
        android:layout_weight="1"
        android:gravity="top"
    />
    <!-- This will be 100% high, 50 wide -->
    <ScrollView
        android:layout_marginLeft="@dimen/between_contents_margin"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        >
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
            <fragment
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:name="org.gvsig.roads.mobile.fragments.itineraries.NewItineraryTabTimesFrag_"
                android:id="@+id/frag_time_new_itinerary"
                tools:layout="@layout/new_itinerary_tab_times_frag"
            />
            <fragment
                android:layout_marginTop="@dimen/between_contents_margin"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:name="org.gvsig.roads.mobile.fragments.itineraries.NewItineraryTabNotesFrag_"
                android:id="@+id/frag_notes_new_itinerary"
                android:layout_below="@+id/frag_time_new_itinerary"
                tools:layout="@layout/new_itinerary_tab_notes_frag"
                android:layout_alignParentEnd="false"
                android:layout_alignParentBottom="false"
            />
        </RelativeLayout>
    </ScrollView>
</LinearLayout>

答案 1 :(得分:0)

试试这个

<LinearLayout 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:layout_gravity="top"
android:gravity="top"
android:orientation="horizontal" >

<fragment
    android:id="@+id/frag_reference_new_itinerary"
    android:name="in.wptrafficanalyzer.achartenginepiechart.NewItineraryTabReferenceFrag_"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="top" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <fragment
            android:id="@+id/frag_time_new_itinerary"
            android:name="in.wptrafficanalyzer.achartenginepiechart.NewItineraryTabTimesFrag_"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <fragment
            android:id="@+id/frag_notes_new_itinerary"
            android:name="in.wptrafficanalyzer.achartenginepiechart.NewItineraryTabNotesFrag_"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="false"
            android:layout_below="@+id/frag_time_new_itinerary" />
    </RelativeLayout>
</ScrollView>