线性布局Android的布局重量? [英] Layout Weight in Linear Layout Android?
问题描述
我使用我的布局2的ListView。并具有当我运行我的code我的头越来越breaked不来全在我的设备,我的页脚不显示,因为在ListView固定头和footer.But。
这是具有ListView中我的主XML文件
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =match_parent
机器人:layout_height =match_parent> <的LinearLayout
机器人:ID =@ + ID / llHeader
机器人:方向=垂直
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =WRAP_CONTENT
机器人:layout_weight =1>
<的LinearLayout
机器人:ID =@ + ID / mainLinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentTop =真
机器人:背景=@绘制/ top_bg
机器人:方向=横向>
< ImageView的
机器人:ID =@ + ID / back_button
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:SRC =@绘制/ back_btn
机器人:layout_marginTop =5DP
机器人:layout_marginLeft =10dp/> <的TextView
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginTop =10dp
机器人:layout_marginLeft =75dp
机器人:文字=机票
机器人:文字颜色=@机器人:彩色/白/>
< / LinearLayout中> <的LinearLayout
机器人:ID =@ + ID / flightDetailsLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =70dp
机器人:layout_below =@ + ID / mainLinearLayout
机器人:layout_centerHorizontal =真
机器人:背景=@绘制/ gray_bg123
机器人:方向=垂直>
<的LinearLayout
机器人:ID =@ + ID / innerflightDetailsLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向> <的TextView
机器人:ID =@ + ID /来自
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginTop =12dp
机器人:paddingLeft =12dp
机器人:TEXTSIZE =12SP
机器人:文字=波士顿到旧金山,
机器人:文字样式=大胆/> <的TextView
机器人:ID =@ + ID / TRIPTYPE
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginTop =12dp
机器人:文字=往返
机器人:TEXTSIZE =12SP/> < / LinearLayout中>
<的LinearLayout
机器人:ID =@ + ID / innerTripLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向> <的TextView
机器人:ID =@ + ID / tripDate
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:paddingLeft =12dp
机器人:TEXTSIZE =10SP
机器人:文字=2013年1月19日
机器人:文字样式=大胆/>
<的TextView
机器人:ID =@ + ID / paxText
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=1成人,0童车,澳婴儿
机器人:TEXTSIZE =12dp/>
< / LinearLayout中> <的LinearLayout
机器人:ID =@ + ID /书
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:layout_marginTop =5DP
机器人:方向=横向> <的TextView
机器人:ID =@ + ID / tripDate
机器人:layout_width =84dp
机器人:layout_height =WRAP_CONTENT
机器人:paddingLeft =12dp
机器人:文字=RS 10,2345
机器人:TEXTSIZE =10SP
机器人:layout_weight =1
机器人:文字样式=大胆/> < ImageView的
机器人:ID =@ + ID / bookBtn
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
机器人:重力=左
机器人:SRC =@绘制/书/> < / LinearLayout中> < / LinearLayout中> <的LinearLayout
机器人:ID =@ + ID / sortFlightLayouts
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ + ID / flightDetailsLayout
机器人:背景=@绘制/ gray_bg123
机器人:方向=横向> <的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
机器人:方向=垂直> <的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=DEL-BLR
机器人:layout_marginLeft =6DP
机器人:layout_marginTop =5DP
机器人:文字样式=大胆/> <的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT 机器人:方向=横向> < ImageView的
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginTop =5DP
机器人:SRC =@绘制/ flight_icon_result/> < ImageView的
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginTop =5DP
机器人:layout_marginLeft =25dp
机器人:SRC =@绘制/时间/> < ImageView的
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginTop =5DP
机器人:layout_marginLeft =5DP
机器人:SRC =@绘制/卢比/>
< / LinearLayout中>
< / LinearLayout中>
<查看
机器人:layout_width =1DP
机器人:layout_height =60dp
机器人:背景=@机器人:彩色/ darker_gray/>
<的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
机器人:背景=@绘制/ gray_bg123
机器人:方向=垂直> <的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=BLR-DEL
机器人:layout_marginLeft =6DP
机器人:layout_marginTop =5DP
机器人:文字样式=大胆/> <的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向> < ImageView的
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginTop =5DP
机器人:SRC =@绘制/ flight_icon_result/> < ImageView的
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginTop =5DP
机器人:layout_marginLeft =25dp
机器人:SRC =@绘制/时间/> < ImageView的
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginTop =5DP
机器人:layout_marginLeft =5DP
机器人:SRC =@绘制/卢比/>
< / LinearLayout中>
< / LinearLayout中>
< / LinearLayout中>
< / LinearLayout中> <的LinearLayout
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =WRAP_CONTENT
机器人:layout_weight =4
机器人:方向=横向> < ListView控件
机器人:ID =@ + ID / lvDepartures
机器人:layout_weight =1
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =WRAP_CONTENT/>
< ListView控件
机器人:ID =@ + ID / lvArrivals
机器人:layout_weight =1
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =WRAP_CONTENT/> < / LinearLayout中> <的LinearLayout
机器人:ID =@ + ID / llFooter
机器人:方向=垂直
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =WRAP_CONTENT
机器人:layout_weight =1/>
<的LinearLayout
机器人:ID =@ + ID / footerLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentBottom =真
机器人:方向=横向
机器人:背景=@绘制/ top_bg> < ImageView的
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
机器人:比重=中心
机器人:layout_marginTop =5DP
机器人:SRC =@绘制/过滤器/>
< ImageView的
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
机器人:比重=中心
机器人:layout_marginTop =5DP
机器人:SRC =@绘制/排序/>
< / LinearLayout中>
< / LinearLayout中>
和本我的布局,我在ListView控件在运行时将
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT> < ImageView的
机器人:ID =@ + ID / flightLogo
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentLeft =真
机器人:layout_alignParentTop =真
机器人:layout_marginBottom =5SP
机器人:layout_marginRight =10SP
机器人:layout_marginTop =5SP
机器人:SRC =@绘制/ SpiceJet航空公司/> <的TextView
机器人:ID =@ + ID / flightCompanyName
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ + ID / flightLogo
机器人:文字=辣妹喷气/> <的TextView
机器人:ID =@ + ID / FLIGHTNUMBER
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ + ID / flightCompanyName
机器人:文字=9W-123/> <的TextView
机器人:ID =@ + ID / flightTime
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_toRightOf =@ + ID / flightLogo
机器人:重力=center_vertical
机器人:文字=6:00 - 7:00/> <的TextView
机器人:ID =@ + ID / flightStop
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ + ID / flightTime
机器人:layout_toRightOf =@ + ID / flightLogo
机器人:文字=1小时35米|不停/> <的TextView
机器人:ID =@ + ID /量
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ + ID / flightStop
机器人:layout_toRightOf =@ + ID / flightLogo
机器人:文字=20,000卢比/>< / RelativeLayout的>
我不知道如果这是你唯一的问题还没有,但是当你使用 layout_weight
的内垂直的LinearLayout
那么你的 layout_height
应 0dp
。
同样,如果它是一个水平的LinearLayout
那么 layout_width
应 0dp内
。此外,所有的布局
如果您正在使用它thenm的人都应该有一个重量
。
的android:layout_below =
在你的第三的LinearLayout
,我相信这不是一个属性的LinearLayout
。这就是 RelativeLayout的
的一个属性。
我给这类型的设置的建议是使用 RelativeLayout的
作为根布局
。然后,你的头的LinearLayout
您可以使用的android:layout_alignParentTop =真正的
和页脚使用的android:layout_alignParentBottom =真正的
然后坚持你的的ListView
与 Android的中间:layout_below =@ ID / idOfHeaderLL
< RelativeLayout的
...>
<的LinearLayout
机器人:ID =@ + ID / headerLL
机器人:layout_alignParentTop =真
...>
<! - 添加标题veiws - >
< / LinearLayout中
< ListView控件
机器人:layout_below =@ ID / headerLL
... />
<的LinearLayout
机器人:layout_alignParentBottom =真
...>
<! - 添加脚注的观点 - >
< / LinearLayout中>
< / RelativeLayout的>
我不打算重写整个布局,但应该给你一个想法。该...是,你会把你的其他属性,如高度,宽度等...
I am using two ListView in my Layout. 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 .
This is my main XML file that is having the 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
<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
.
Likewise if it is inside of a horizontal LinearLayout
then the layout_width
should be 0dp
. Also, all of the layouts
should have a weight
if you are using it on one of thenm.
android:layout_below=
in your third LinearLayout
, I believe, is not a property of LinearLayout
. That is a property of RelativeLayout
.
My suggestion for this type of setup is to use RelativeLayout
as the root 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"
<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...
这篇关于线性布局Android的布局重量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!