帮助我进行XML布局。
我在活动中有三个LinearLayout,所有LinearLayout都有垂直的位置(从上到下)
我的LinearLayout定位
- 第一个LinearLayout具有android:layout_height="30px",android:layout_width="fill_rent"
- second LinearLayout具有android:layout_height="fill_parent",android:layout_width="fill_parent"
- 第三行布局具有android:layout_height="30px",android:layout_width="fill_rent"
但是,当第二个LinearLayout设置为fill_present时,它会填充整个屏幕(从第一个Layout到屏幕底部),第三个Linear布局无法显示!
我需要如何填充第二个布局?
帮助我
简单地使用这个。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MainLinear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="#fbfbfb"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:orientation="horizontal" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.9"
android:orientation="vertical" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:orientation="horizontal" />
</LinearLayout>
</LinearLayout>
您可以根据自己的目的使用相对布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="@+id/linearLayout1"
android:layout_above="@+id/linearLayout2"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
诀窍是不使用fill_parent
,而是使用0dp
,并使用android:layout_weight="1"
对其进行加权。这意味着它将占用所有可用的额外空间
您必须重视中间的线性布局,这样它才能占据整个高度。
try this for middle linear layout,
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</LinearLayout>
如果将height="fill_present"设置为中间布局,它将占用父视图的高度,因此第三个视图将不可见(在屏幕外)
1st layout : android:layout_height="30px", android:layout_width="match_parent"
2nd layout : android:layout_height="0dp", android:layout_width="match_parent", android:layout_weight="1"
3rd layout : android:layout_height="30px", android:layout_width="match_parent"
您应该使用"dp"而不是"px",以便在不同的屏幕密度上获得相同的大小。
(不赞成使用"fill_parent",应该使用"match_parent",它也一样)