我真的在努力了解如何使线性布局在xml活动中采用正确的比例。
下面是我的代码。基本上有3层(标记为第一层,第二层和第三层)。我希望它们垂直对齐,以便第1层占据屏幕顶部的10%,随后的2占据屏幕的下一个10%,然后第3层占据屏幕的其余80%。
使用下面的代码,顶层2层中的小部件被压缩,因此我看不到完整的小部件。我确定这与我使用布局权重的方式有关,但我不确定如何解决它。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="@+id/layTopLevel"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/colorPrimaryDark"
android:weightSum="10"
android:orientation="vertical">
<!--layer one-->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/layerOne"
android:gravity="center_vertical"
android:layout_weight="1">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/customborder"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_weight="0.5"
>
<LinearLayout
android:id="@+id/layoutName"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="@drawable/customborder"
android:orientation="vertical">
<TextView
android:id="@+id/txtMyPlaceName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/myPlaceName" />
<EditText
android:id="@+id/etMyPlaceName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter name here"
android:singleLine="true" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_weight="0.5"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/layoutType"
android:layout_weight="1"
android:background="@drawable/customborder"
android:orientation="vertical" >
<TextView
android:id="@+id/txtMyPlaceType"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="@string/myPlaceType" />
<Spinner
android:id="@+id/spnType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style = "@style/Widget.AppCompat.Spinner.Underlined"
/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
<!--Layer Two-->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/layerTwo"
android:gravity="center_vertical"
android:layout_weight="1">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="@drawable/customborder"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_weight="0.333">
<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left|center_vertical"
android:onClick="addSightingToDb"
android:text="@string/addSighting" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_weight="0.333">
<Button
android:id="@+id/btnClearLast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left|center_vertical"
android:onClick="addSightingToDb"
android:text="@string/addSighting" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_weight="0.334">
<Button
android:id="@+id/btnClearAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left|center_vertical"
android:onClick="addSightingToDb"
android:text="@string/addSighting" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
<!--Layer three-->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/layerThree"
android:gravity="center_vertical"
android:layout_weight="8">
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
</LinearLayout>
<!--<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="fill_parent"-->
<!--xmlns:app="http://schemas.android.com/apk/res-auto"-->
<!--android:layout_marginTop="10dp"-->
<!--android:background="@color/colorPrimaryDark"-->
<!--android:orientation="horizontal">-->
<!--<LinearLayout-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="fill_parent"-->
<!--android:orientation="vertical">-->
<!--<LinearLayout-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="fill_parent"-->
<!--android:layout_weight="1"-->
<!--android:background="@drawable/customborder"-->
<!--android:orientation="vertical" >-->
<!--<!–first row–>-->
<!--<LinearLayout-->
<!--android:layout_width="fill_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_weight="0.5"-->
<!--android:background="@drawable/customborder"-->
<!--android:layout_marginBottom="5dp"-->
<!--android:orientation="horizontal" >-->
<!--<RelativeLayout-->
<!--android:layout_width="fill_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:gravity="center_vertical"-->
<!--android:layout_weight="0.50">-->
<!--<LinearLayout-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_weight="1"-->
<!--android:background="@drawable/customborder"-->
<!--android:orientation="vertical" >-->
<!--<TextView-->
<!--android:id="@+id/txtMyPlaceName"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="@string/myPlaceName" />-->
<!--<EditText-->
<!--android:id="@+id/etMyPlaceName"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:singleLine="true" />-->
<!--</LinearLayout>-->
<!--</RelativeLayout>-->
<!--<RelativeLayout-->
<!--android:layout_width="fill_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:gravity="center_vertical"-->
<!--android:layout_weight="0.50">-->
<!--<LinearLayout-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_weight="1"-->
<!--android:background="@drawable/customborder"-->
<!--android:orientation="vertical" >-->
<!--<TextView-->
<!--android:id="@+id/txtMyPlaceType"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="@string/myPlaceType" />-->
<!--<Spinner-->
<!--android:id="@+id/spnSeenHeard"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--style = "@style/Widget.AppCompat.Spinner.Underlined"-->
<!--/>-->
<!--</LinearLayout>-->
<!--</RelativeLayout>-->
<!--</LinearLayout>-->
<!--<!–button row–>-->
<!--<LinearLayout-->
<!--android:layout_width="fill_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_weight="0.50"-->
<!--android:background="@drawable/customborder"-->
<!--android:layout_marginBottom="10dp"-->
<!--android:orientation="horizontal" >-->
<!--<RelativeLayout-->
<!--android:layout_width="fill_parent"-->
<!--android:layout_height="match_parent"-->
<!--android:gravity="center_vertical"-->
<!--android:layout_weight="0.34">-->
<!--<Button-->
<!--android:id="@+id/btnAdd"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:gravity="left|center_vertical"-->
<!--android:onClick="addSightingToDb"-->
<!--android:text="@string/addSighting" />-->
<!--</RelativeLayout>-->
<!--<RelativeLayout-->
<!--android:layout_width="fill_parent"-->
<!--android:layout_height="match_parent"-->
<!--android:gravity="center_vertical"-->
<!--android:layout_weight="0.33">-->
<!--<Button-->
<!--android:id="@+id/btnClearLast"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:gravity="left|center_vertical"-->
<!--android:onClick="addSightingToDb"-->
<!--android:text="@string/addSighting" />-->
<!--</RelativeLayout>-->
<!--<RelativeLayout-->
<!--android:layout_width="fill_parent"-->
<!--android:layout_height="match_parent"-->
<!--android:gravity="center_vertical"-->
<!--android:layout_weight="0.33">-->
<!--<Button-->
<!--android:id="@+id/btnClearAll"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:gravity="left|center_vertical"-->
<!--android:onClick="addSightingToDb"-->
<!--android:text="@string/addSighting" />-->
<!--</RelativeLayout>-->
<!--</LinearLayout>-->
<!--<!–Map row–>-->
<!--<LinearLayout-->
<!--android:id="@+id/layout_map"-->
<!--android:layout_width="fill_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_weight="0.50"-->
<!--android:background="@drawable/customborder"-->
<!--android:layout_marginBottom="5dp"-->
<!--android:orientation="horizontal" >-->
<!--<RelativeLayout-->
<!--android:id="@+id/map_layout"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent">-->
<!--<fragment-->
<!--android:id="@+id/map"-->
<!--android:name="com.google.android.gms.maps.SupportMapFragment"-->
<!--android:layout_width="fill_parent"-->
<!--android:layout_height="match_parent"-->
<!--android:layout_alignParentStart="true"-->
<!--android:layout_alignParentTop="true" />-->
<!--</RelativeLayout>-->
<!--</LinearLayout>-->
<!--</LinearLayout>-->
<!--</LinearLayout>-->
<!--</LinearLayout>-->
在视图上使用权重时,将限制视图的高度。
如果所需的视图高度超过了android:layout_weight属性为其分配的高度,则将压缩其子视图以遵守layout_weight属性。
删除所有图层上的layout_weight属性。
将第一层高度设置为wrap_content
将第二层高度设置为wrap_content
将三层高度设置为match_parent。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句