【问题标题】:Why my element is not staying at the bottom as supposed?为什么我的元素没有按预期停留在底部?
【发布时间】:2015-02-08 08:34:54
【问题描述】:

我有这个布局 xml 文件:

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

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity">

        <TextView
            android:layout_marginTop="40dp"
            android:id="@+id/emalLbl"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="@string/test" />

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/testTxtLay"
            android:layout_marginTop="10dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/emalLbl"
            android:orientation="horizontal"
            android:padding="10dp"
            android:weightSum="1">

            <EditText
                android:id="@+id/testTxt"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:background="@drawable/edit_text_design" />
        </LinearLayout>

        <TextView
            android:layout_marginTop="60dp"
            android:id="@+id/reminderLbl"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/testTxtLay"
            android:layout_centerHorizontal="true"
            android:text="@string/reminder" />

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_marginTop="10dp"
            android:id="@+id/reminderTxtLay"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/reminderLbl"
            android:orientation="horizontal"
            android:padding="10dp"
            android:weightSum="1">

            <EditText
                android:id="@+id/reminderTxt"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.0"
                android:lines="2"
                android:background="@drawable/edit_text_design" />
        </LinearLayout>

        <View
            android:id="@+id/spacer"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_below="@id/reminderTxtLay"
            android:layout_centerHorizontal="true" />

        <Button
            android:layout_marginTop="200dp"
            android:id="@+id/pickDateBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@id/reminderTxtLay"
            android:layout_toLeftOf="@+id/spacer"
            android:onClick="showDatePickerDialog"
            android:text="@string/datePickerBtnTxt" />

        <Button
            android:id="@+id/pickTimeBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@id/pickDateBtn"
            android:layout_alignParentRight="true"
            android:layout_toRightOf="@+id/pickDateBtn"
            android:onClick="showTimePickerDialog"
            android:text="@string/pickTimeBtn" />

        <View
            android:id="@+id/spacerTxt"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_below="@id/reminderTxtLay"
            android:layout_centerHorizontal="true" />

        <EditText
            android:layout_marginTop="15dp"
            android:id="@+id/selectedDate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@id/spacerTxt"
            android:layout_below="@id/pickDateBtn"
            android:layout_toLeftOf="@+id/spacerTxt" />

        <EditText
            android:id="@+id/selectedTime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@id/selectedDate"
            android:layout_below="@id/pickTimeBtn"
            android:layout_toRightOf="@+id/selectedDate" />

        <Button
            android:id="@+id/submitBtn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerInParent="true"
            android:onClick="submitData"
            android:text="@string/submitBtn" />

    </RelativeLayout>
</ScrollView>

这是7寸平板的定制设计,放在res\layout-sw600dp

无论如何,在 android studio 的横向预览中看起来还不错:

但是在模拟器中出现了问题,这看起来很奇怪。我的意思是提交按钮不在底部,pickDatepickTime 按钮在布局底部。

我知道我在这里遗漏了一个基本点,但作为一名 android 开发人员,我无法发现它。

你能帮我推一下吗?

【问题讨论】:

  • 你想用提交按钮上的父中心来完成什么?
  • @karaokyo 将其居中
  • 如果要垂直居中,不能与父底部对齐。如果你想水平居中,它已经匹配父宽度。
  • 你应该使用线性布局
  • 始终在 ScrollView 中使用 LinearLayout 而不是相对布局。如果您想提交按钮以对齐屏幕按钮,请使用 RelativeLayout 作为根视图

标签: android xml android-layout android-relativelayout


【解决方案1】:

这里有一个完整的方法来完成你似乎想要完成的事情。您不需要为“空间”等创建任何视图。您只需在视图的任一侧添加边距或填充,使其远离另一个视图。

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

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">
    <LinearLayout
        android:id="@+id/linear_wrapper"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center_horizontal"
        >

        <TextView
            android:layout_marginTop="40dp"
            android:id="@+id/emalLbl"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/test" />


        <EditText
            android:id="@+id/testTxt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/edit_text_design" />

        <TextView
            android:layout_marginTop="60dp"
            android:id="@+id/reminderLbl"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/reminder" />


        <EditText
            android:id="@+id/reminderTxt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:lines="2"
            android:background="@drawable/edit_text_design" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="200dp"
            >
            <Button
                android:id="@+id/pickDateBtn"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="showDatePickerDialog"
                android:text="@string/datePickerBtnTxt" />

            <Button
                android:id="@+id/pickTimeBtn"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="showTimePickerDialog"
                android:text="@string/pickTimeBtn" />
        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <EditText
                android:id="@+id/selectedDate"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

            <EditText
                android:id="@+id/selectedTime"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
        </LinearLayout>

    </LinearLayout>

    <Button
        android:id="@+id/submitBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/linear_wrapper"
        android:onClick="submitData"
        android:text="@string/submitBtn" />
</RelativeLayout>

【讨论】:

  • 这样selectedDateselectedTime 就隐藏在提交按钮下。
  • 你是对的,忘记在提交按钮上添加以下内容。它现在编辑。您也可以将提交按钮放在底部的外部 LinearLayout 中,它始终位于所有视图的底部,但不一定位于屏幕底部。一切都取决于您的屏幕尺寸。
  • 但是现在不在横向模式下就消失了。 :(
  • 真的吗?对我来说看起来不错。如果向下滚动它不会显示?
  • 是的,它不存在。只是错过了
【解决方案2】:

您可以将 2 个按钮并排放置!

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp"
        android:weightSum="1">

        <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:gravity="left"
            android:layout_height="wrap_content"
            android:lines="2" />

        <Button
            android:id="@+id/btn2"
            android:layout_width="fill_parent"    
            android:gravity="right"
            android:layout_height="wrap_content" />

    </LinearLayout>

【讨论】:

    猜你喜欢
    • 2018-05-14
    • 2015-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 2020-02-29
    • 2023-03-18
    相关资源
    最近更新 更多