【问题标题】:Android RelativeLayout and button problemAndroid RelativeLayout 和按钮问题
【发布时间】:2011-09-28 06:49:48
【问题描述】:

我的布局可以在纵向模式下正常工作。但是在横向模式下它不会像我想要的那样显示:

Portrait mode

Landscape mode

这是 .xml 文件的概要:

<RelativeLayout>

    <LinearLayout android:orientation="horizontal">
        <TextView />
        <TextView />
    </LinearLayout>

    <TextView />

    <LinearLayout android:orientation="vertical">

        <LinearLayout android:orientation="horizontal">

            <LinearLayout android:orientation="vertical">
                <TextView />
                <TextView />
            </LinearLayout>

            <LinearLayout android:orientation="vertical">
                <Spinner />
                <Spinner />
            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

    <Button/>

</RelativeLayout>

这是实际的 .xml 文件

<?xml version="1.0" encoding="utf-8"?>

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

    <LinearLayout android:id="@+id/departure_block"
        android:orientation="horizontal" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:layout_marginTop="40px">

        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:textSize="17sp"
            android:text="You are departing from " />

        <TextView android:id="@+id/editTextDepartureStation"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="" android:layout_weight="2" android:textSize="17sp" />
    </LinearLayout>

    <TextView android:id="@+id/info_block" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:textSize="17sp"
        android:layout_below="@id/departure_block" android:text="Please select remaining journey details: "
        android:layout_marginTop="40px" />

    <LinearLayout android:orientation="vertical"
        android:layout_width="fill_parent" android:layout_below="@id/info_block"
        android:layout_height="fill_parent">

        <!-- Labels and widgets -->
        <LinearLayout android:orientation="horizontal"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:layout_marginTop="20px">

            <!-- Labels -->
            <LinearLayout android:orientation="vertical"
                android:layout_width="fill_parent" android:layout_height="wrap_content"
                android:layout_weight="2" android:paddingBottom="10px">

                <TextView android:layout_width="wrap_content"
                    android:layout_height="70px" android:text="@string/destinationStation"
                    android:layout_weight="2" android:paddingTop="15px"
                    android:textSize="17sp" />

                <TextView android:layout_width="wrap_content"
                    android:layout_height="70px" android:text="@string/numTickets"
                    android:layout_weight="2" android:paddingTop="15px"
                    android:textSize="17sp" />
            </LinearLayout>
            <!-- End labels -->

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

                <Spinner android:id="@+id/spinnerDestinationStation"
                    android:layout_width="fill_parent" android:layout_height="wrap_content"
                    android:prompt="@string/destination_prompt" android:layout_weight="1"
                    android:textSize="17sp" />


                <Spinner android:id="@+id/spinnerNumTickets"
                    android:layout_width="fill_parent" android:layout_height="wrap_content"
                    android:prompt="@string/numTickets_prompt" android:layout_weight="1"
                    android:textSize="17sp" />
            </LinearLayout>
            <!-- End widgets -->

        </LinearLayout>
        <!-- End labels and widgets -->

    </LinearLayout>


    <Button android:id="@+id/buttonPurchase" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="Purchase ticket"
        android:layout_weight="1" android:textSize="17sp" android:background="@drawable/custom_button"
        android:textColor="#ffffff" android:shadowColor="#000000"
        android:shadowRadius="1.5" android:layout_gravity="bottom"
        android:layout_alignParentBottom="true" android:shadowDx="1"
        android:shadowDy="1" android:layout_margin="10px" />


</RelativeLayout>

我理解为什么按钮出现在微调器的顶部(它正确地与父级 RelativeLayout 的底部对齐),但我无法创建可以阻止重叠问题的 xml。

我想要任何滚动或 ScrollView 来封装按钮。如果按钮出现在显示屏底部,这不是问题——只要它可以通过滚动访问。这是我努力实现的目标。

谁能帮忙?

非常感谢。

【问题讨论】:

    标签: android xml button android-layout android-relativelayout


    【解决方案1】:

    您的布局对于屏幕来说太大了,因此您必须将 info_block 包裹在 ScrollView 中,并将其与顶部对齐,并将其底部与按钮顶部对齐。

    这意味着您必须在 info_block 之前定义 Button,以便您可以将 info_block 的底部与按钮的顶部对齐。

    为了澄清,问题是你的 info_block 的底部边缘没有绑定到任何东西,所以绘制按钮,你将有一个边缘可以放置。您应该始终首先在相对布局中绘制固定元素,以便为未绑定的元素提供位置。

    【讨论】:

    • 你不需要把它放在 ScrollView 上,因为相对布局可以滚动
    • 他希望按钮作为布局的一部分固定在底部。那么在这种情况下你会如何滚动 info_block 呢?
    【解决方案2】:

    也许你最好的选择是让相对布局可以滚动,我相信它被称为?这样用户就可以用手指上下移动整个布局。

    【讨论】:

      【解决方案3】:

      我明天必须检查,但 fillViewport 似乎是我想要的 ScrollView 属性。解释于:ScrollView’s handy trick

      编辑添加这个确实解决了我的问题。两个重要的事情是:

      1. 要在 ScrollView 中使用 fillViewport="true",这可以确保子元素展开以填充显示。仅此还不够,接下来要确保:

      2. 最后一个元素 before Button 具有 android:layout_weight="1.0" 以确保它填充由具有 fillViewport="true" 的 ScrollView 创建的空间。

        李>

      【讨论】:

        猜你喜欢
        • 2011-03-20
        • 1970-01-01
        • 2011-01-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-02
        相关资源
        最近更新 更多