简体   繁体   English

Linear Layout Android中的布局重量?

[英]Layout Weight in Linear Layout Android?

I am using two ListView in my Layout. 我在我的布局中使用了两个ListView。 and having fixed header and footer.But when i am running my code my header is getting breaked not coming full on my Device and my footer is not showing because of the ListView . 并且具有固定的页眉和页脚。但是当我运行我的代码时,我的标题因为ListView而无法在我的设备上显示并且我的页脚没有显示。

This is my main XML file that is having the ListView 这是我的主要XML文件,它具有ListView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/llHeader"
        android:orientation="vertical"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_weight="1">
        <LinearLayout
            android:id="@+id/mainLinearLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:background="@drawable/top_bg"
            android:orientation="horizontal" >
        <ImageView 
            android:id="@+id/back_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/back_btn"
            android:layout_marginTop="5dp"
            android:layout_marginLeft="10dp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="75dp"
            android:text="Flights"
            android:textColor="@android:color/white" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/flightDetailsLayout"
        android:layout_width="fill_parent"
        android:layout_height="70dp"
        android:layout_below="@+id/mainLinearLayout"
        android:layout_centerHorizontal="true"
        android:background="@drawable/gray_bg123"
        android:orientation="vertical" >
        <LinearLayout
            android:id="@+id/innerflightDetailsLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/from"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="12dp"
                android:paddingLeft="12dp"
                android:textSize="12sp"
                android:text="Boston to San Fransisco, "
                android:textStyle="bold" />

            <TextView
                android:id="@+id/tripType"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="12dp"
                android:text="RoundTrip"
                android:textSize="12sp" />

        </LinearLayout>
        <LinearLayout
            android:id="@+id/innerTripLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/tripDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="12dp"
                android:textSize="10sp"
                android:text="19 Jan 2013 " 
                android:textStyle="bold"/>
            <TextView
                android:id="@+id/paxText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="for 1 Adults,0 Childs,O Infants"
                android:textSize="12dp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/Book"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginTop="5dp"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/tripDate"
                android:layout_width="84dp"
                android:layout_height="wrap_content"
                android:paddingLeft="12dp"
                android:text="Rs 10,2345 "
                android:textSize="10sp"
                android:layout_weight="1"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/bookBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="left"
                android:src="@drawable/book" />

        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/sortFlightLayouts"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/flightDetailsLayout"
        android:background="@drawable/gray_bg123"
        android:orientation="horizontal" >

    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="DEL-BLR"
                android:layout_marginLeft="6dp"
                android:layout_marginTop="5dp"
                android:textStyle="bold" />

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"

                android:orientation="horizontal" >

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:src="@drawable/flight_icon_result" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:layout_marginLeft="25dp"
                    android:src="@drawable/time" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:layout_marginLeft="5dp"
                    android:src="@drawable/rupee" />
            </LinearLayout>
        </LinearLayout>
        <View
            android:layout_width="1dp"
            android:layout_height="60dp"
            android:background="@android:color/darker_gray" />
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/gray_bg123"
            android:orientation="vertical" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="BLR-DEL"
                android:layout_marginLeft="6dp"
                android:layout_marginTop="5dp"
                android:textStyle="bold" />

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:src="@drawable/flight_icon_result" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:layout_marginLeft="25dp"
                    android:src="@drawable/time" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:layout_marginLeft="5dp"
                    android:src="@drawable/rupee" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
        </LinearLayout>

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

        <ListView
            android:id="@+id/lvDepartures"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>
        <ListView
             android:id="@+id/lvArrivals"
             android:layout_weight="1"
             android:layout_height="wrap_content"
             android:layout_width="wrap_content"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/llFooter"
        android:orientation="vertical"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_weight="1"/>
        <LinearLayout
        android:id="@+id/footerLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:background="@drawable/top_bg" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_marginTop="5dp"
            android:src="@drawable/filter" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_marginTop="5dp"
            android:src="@drawable/sort" />
    </LinearLayout>
</LinearLayout> 

And this my Layout that i am putting in the ListView at RunTime 这是我在RunTime中放入ListView的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/flightLogo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="5sp"
        android:layout_marginRight="10sp"
        android:layout_marginTop="5sp"
        android:src="@drawable/spicejet" />

    <TextView
        android:id="@+id/flightCompanyName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/flightLogo"
        android:text="Spice Jet" />

    <TextView
        android:id="@+id/flightNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/flightCompanyName"
        android:text="9W-123" />

    <TextView
        android:id="@+id/flightTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/flightLogo"
        android:gravity="center_vertical"
        android:text="6:00 - 7:00" />

    <TextView
        android:id="@+id/flightStop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/flightTime"
        android:layout_toRightOf="@+id/flightLogo"
        android:text="1h 35m | Non Stop" />

    <TextView
        android:id="@+id/amount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/flightStop"
        android:layout_toRightOf="@+id/flightLogo"
        android:text="Rs 20,000" />

</RelativeLayout>

I can't tell if this is your only problem yet but when you use layout_weight inside of a vertical LinearLayout then your layout_height should be 0dp . 我不知道这是否是你唯一的问题但是当你在一个垂直的LinearLayout使用layout_weight时,你的layout_height应该是0dp

Likewise if it is inside of a horizontal LinearLayout then the layout_width should be 0dp . 同样,如果它在水平LinearLayout内,则layout_width应为0dp Also, all of the layouts should have a weight if you are using it on one of thenm. 此外,如果您在其中一个上使用它,所有layouts都应该有一个weight

android:layout_below= in your third LinearLayout , I believe, is not a property of LinearLayout . android:layout_below=在你的第三个LinearLayout ,我相信,它不是LinearLayout的属性。 That is a property of RelativeLayout . 这是RelativeLayout的属性。

My suggestion for this type of setup is to use RelativeLayout as the root layout . 我对这种设置的建议是使用RelativeLayout作为根layout Then, for your header LinearLayout you can use android:layout_alignParentTop="true" and for your footer use android:layout_alignParentBottom="true" then stick your ListView in the middle with android:layout_below="@id/idOfHeaderLL" 然后,对于您的标题LinearLayout您可以使用android:layout_alignParentTop="true"并为您的页脚使用android:layout_alignParentBottom="true"然后将您的ListView放在中间,使用android:layout_below="@id/idOfHeaderLL"

<RelativeLayout
  ...>
    <LinearLayout
      android:id="@+id/headerLL"
      android:layout_alignParentTop="true"
      ...>
     <!-- add header veiws -->
    </LinearLayout
    <ListView
       android:layout_below="@id/headerLL"
       .../>
    <LinearLayout
       android:layout_alignParentBottom="true"
       ...>
    <!-- add footer views -->
    </LinearLayout>
</RelativeLayout>

I'm not going to rewrite the whole layout but that should give you an idea. 我不打算重写整个布局,但这应该给你一个想法。 The "..." is where you will put in your other properties such as height, width, etc... “......”是您放入其他属性的地方,如高度,宽度等......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM