首页 文章

与RelativeLayout底部对齐的按钮被切断

提问于
浏览
3

当我使用 android:layout_alignParentBottom="true" 时,我的RelativeLayout中有一个按钮正被切断,但我尝试的其他任何东西都没有正常工作 . 有任何想法吗?

这是我的布局:

<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:orientation="vertical"
    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="com.cdar.sab.cdar.MainActivity$PlaceholderFragment">

    <TextView
        android:id="@+id/section_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/diagnostics_title" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:ems="10"
        android:layout_below="@+id/section_label"
        android:layout_alignParentStart="true"
        android:layout_marginTop="42dp"
        android:hint="@string/email_hint"
        android:id="@+id/emailTB" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
        android:ems="10"
        android:layout_below="@+id/emailTB"
        android:layout_alignParentStart="true"
        android:layout_marginTop="45dp"
        android:hint="@string/message_hint"
        android:maxLines="10"
        android:id="@+id/messageTB" />

    <Button
        android:text="Button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

9 回答

  • 1

    很难理解为什么UI会被扭曲,原因可能有很多,但我非常怀疑文本的限制是否会超出导致UI失真的输入框中 . 请添加屏幕截图以便更好地理解所以根据它,我试图减少视图之间的文本大小和边距,以确保UI不会扭曲,如果这是由于几个边缘 .

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">
    
    <TextView
        android:id="@+id/section_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Diagnostic"
        android:textSize="12sp"/>
    
    <EditText
        android:id="@+id/emailTB"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/section_label"
        android:layout_marginTop="22dp"
        android:ems="10"
        android:hint="Hint"
        android:inputType="textEmailAddress"
        android:textSize="12sp"/>
    
    <EditText
        android:id="@+id/messageTB"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/emailTB"
        android:layout_marginTop="25dp"
        android:ems="10"
        android:hint="Hint"
        android:inputType="textMultiLine"
        android:maxLines="10"
        android:textSize="12sp"/>
    
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Button" />
    
    </RelativeLayout>
    
  • 1

    您已在主容器中指定了布局填充

    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    

    添加边距而不是填充将解决您的问题 . 尝试下面的代码

    android:marginBottom="@dimen/activity_vertical_margin"
    android:marginLeft="@dimen/activity_horizontal_margin"
    android:marginRight="@dimen/activity_horizontal_margin"
    android:marginTop="@dimen/activity_vertical_margin"
    
  • 0

    请添加屏幕截图以更好地理解问题,如果按钮的一半隐藏在屏幕导航栏后面(因为某些设备有屏幕导航栏和屏幕外的其他设备),然后在RelativeLayout android:fitsSystemWindows="true" 中添加此行,或者您可以使用代码隐藏导航栏 .

  • 0

    我使用LinearLayout修改了你的布局,希望它对你有用 .

    <LinearLayout 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:orientation="vertical"
        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="com.cdar.sab.cdar.MainActivity$PlaceholderFragment">
    
        <TextView
            android:id="@+id/section_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/diagnostics_title" />
    
        <EditText
            android:id="@+id/emailTB"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="42dp"
            android:ems="10"
            android:hint="@string/email_hint"
            android:inputType="textEmailAddress" />
    
        <EditText
            android:id="@+id/messageTB"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginTop="45dp"
            android:layout_weight="1"
            android:ems="10"
            android:hint="@string/message_hint"
            android:inputType="textMultiLine"
            android:maxLines="10" />
    
        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0dp"
            android:text="Button" />
    
    </LinearLayout>
    
  • 0

    我看到你在PlaceholderFragment中使用了布局 . 我在片段中的适配器遇到了同样的问题 . 您应该尝试使用第二个EditText约束按钮,然后运行代码(快速修复) . 我认为问题在于,当您在片段中运行代码时,布局会被移位,并且match_parent将被计算为整个屏幕(或活动的大小),从而将按钮与父Activity对齐,然后将布局向下移动以腾出空间对于标签标签 .
    The layout looks fine

  • 0

    希望这会有所帮助:

    <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:orientation="vertical"
    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="com.cdar.sab.cdar.MainActivity$PlaceholderFragment">
    
    <RelativeLayout
        android:id="@+id/rlContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <TextView
            android:id="@+id/section_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/diagnostics_title" />
    
        <EditText
            android:id="@+id/emailTB"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/section_label"
            android:layout_marginTop="42dp"
            android:ems="10"
            android:hint="@string/email_hint"
            android:inputType="textEmailAddress" />
    
        <EditText
            android:id="@+id/messageTB"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/emailTB"
            android:layout_marginTop="45dp"
            android:ems="10"
            android:hint="@string/message_hint"
            android:inputType="textMultiLine"
            android:maxLines="10" />
    
    </RelativeLayout>
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rlContainer">
    
        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:text="Button" />
    </RelativeLayout>
    
  • 0

    您需要为Button添加外部相对布局

    <RelativeLayout..............>
        <RelativeLayout android:id="@+id/content_rl>
        <!-- Some ELEMENTS -->
        </RelativeLayout>
    
        <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/content_rl">
    
        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:text="Button" />
    </RelativeLayout>
    
  • 0
    <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:orientation="vertical"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp">
    
    <TextView
        android:id="@+id/section_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some Title" />
    
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:ems="10"
        android:layout_below="@+id/section_label"
        android:layout_alignParentStart="true"
        android:layout_marginTop="42dp"
        android:hint="Some hint"
        android:id="@+id/emailTB" />
    
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
        android:ems="10"
        android:layout_below="@+id/emailTB"
        android:layout_alignParentStart="true"
        android:layout_marginTop="45dp"
        android:hint="Some hint"
        android:maxLines="10"
        android:id="@+id/messageTB" />
    
    <RelativeLayout
        android:layout_below="@+id/messageTB"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:gravity="bottom"
        android:layout_height="wrap_content">
        <Button
            android:text="Button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button"
            android:layout_centerInParent="true"/>
    </RelativeLayout>
    

    image

  • -1

    也许你可以尝试将marginBottom给你的Button

    android:layout_marginBottom="8dp"
    

相关问题