Android Studio活动XML无法获取布局权重以正确对齐

乔恩

我真的在努力了解如何使线性布局在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" >-->


                    <!--&lt;!&ndash;first row&ndash;&gt;-->
                    <!--<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>-->



                    <!--&lt;!&ndash;button row&ndash;&gt;-->

                    <!--<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>-->




                    <!--&lt;!&ndash;Map row&ndash;&gt;-->
                    <!--<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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

无法将文本与 imageview android 布局 xml 的右侧对齐

来自分类Dev

Android Studio中没有xml布局的新活动

来自分类Dev

Android布局权重无法按照我的想象

来自分类Dev

Android无法获取布局

来自分类Dev

Android布局权重

来自分类Dev

Android 布局权重故障?

来自分类Dev

如何提供适当的权重以在表 android 中正确对齐视图

来自分类Dev

Android Studio无法将xml文件标识为布局文件

来自分类Dev

无法开始活动Android Studio

来自分类Dev

Android Rating Bar布局权重属性无法正常运行

来自分类Dev

从Android Studio查看时,为什么SDK中的Android布局无法正确显示

来自分类Dev

是否可以分配热键在Android Studio中的“活动”和“ XML”布局之间切换?

来自分类Dev

简单的Android祝酒方式无法正确对齐

来自分类Dev

简单的Android祝酒方式无法正确对齐

来自分类Dev

Textview在Android布局中无法正确包装

来自分类Dev

动态对齐视图权重-Android

来自分类Dev

Android Studio:无法找到明确的活动类,但AndroidManifest.xml中已声明活动

来自分类Dev

xml中的权重-Android

来自分类Dev

Android TV无法启动正确的活动

来自分类Dev

Android布局对齐问题

来自分类Dev

Android对齐布局中心

来自分类Dev

Android对齐布局中心

来自分类Dev

Android夸大布局对齐

来自分类Dev

从Android中的xml布局获取节点

来自分类Dev

Android Studio:InvalidVirtualFileAccessException尝试呈现XML布局

来自分类Dev

Android Studio无法渲染布局预览

来自分类Dev

无法在android studio中查看设计布局

来自分类Dev

android设计xml布局对齐方式(如何绘制类似图)

来自分类Dev

无法在Android Studio中添加活动

Related 相关文章

热门标签

归档