【问题标题】:ScrollView inside a RelativeLayout相对布局内的 ScrollView
【发布时间】:2016-07-17 00:18:40
【问题描述】:

我需要在相对布局中进行滚动视图。

当我添加 ScrollView 时,这是 http://postimg.org/image/6abxth299/ 的结果,其中组件只占用了一半的屏幕,当这个屏幕打开时,键盘默认出现

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="vertical" >
    <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"
        android:layout_centerHorizontal="true"
        android:id="@+id/data_entry_layout">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Post Title"
                android:id="@+id/post_title"
                android:layout_alignBaseline="@+id/post_title_input"
                android:layout_alignBottom="@+id/post_title_input"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:textSize="26px"/>

            <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/post_title_input"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_toRightOf="@+id/post_title"
                android:layout_toEndOf="@+id/post_title"
                android:inputType="textCapSentences"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Post Description"
                android:id="@+id/post_description"
                android:layout_below="@+id/post_title_input"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_marginTop="39dp"
                android:textSize="26px"/>

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textMultiLine|textCapSentences"
                android:ems="10"
                android:id="@+id/editText"
                android:layout_marginTop="16dp"
                android:layout_below="@+id/post_description"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"/>

            <ImageButton
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/imageButton1"
                android:layout_below="@+id/editText"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:baselineAlignBottom="false"
                android:contextClickable="true"
                android:src="@drawable/image_button_icon"
                android:onClick="galleryIntent"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Cancel"
                android:id="@+id/cancel"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@id/imageButton1"

               />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Submit"
                android:id="@+id/submit"
                android:layout_alignTop="@+id/cancel"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_below="@id/imageButton1"/>

    </RelativeLayout>
</ScrollView>

【问题讨论】:

  • 永远不要在 ScrollView 中使用 match_parent 作为 RelativeLayout 的高度,将其更改为 wrap_content
  • 改变了它,组件仍然占据了一半的屏幕
  • 你想要什么?
  • 图像按钮的高度需要响应(即)它会根据您使用的设备来占据整个屏幕而变化

标签: android android-layout android-studio scrollview android-relativelayout


【解决方案1】:

scrollview 的第一个 dirsct 子项的高度,无论是相对布局还是线性布局,都应该是 wrap content 而不是匹配父级。此外,内部孩子应该有特定的体重或身高,而不是像你为 imageButton 所做的那样匹配父母。

【讨论】:

  • 将高度更改为 430px 并且它起作用了。但是,我相信它不会对其他设备做出响应,对吧? (即高度不会相应调整)?
  • 你把哪个高度改成了430px?
  • 图片按钮
【解决方案2】:

尝试在您的 AndroidManifest 中添加以下内容,以删除默认情况下出现的键盘

android:windowSoftInputMode="stateAlwaysHidden|adjustResize"

并使用 android:layout_height="wrap_content" 进行滚动视图,建议将 LinearLayout垂直方向 布局一起使用。

【讨论】:

    【解决方案3】:

    我认为 ScrollView 在 Relativelayout 中不起作用。最好使用LinearLayout。

    【讨论】:

      【解决方案4】:

      试试这个...

      <?xml version="1.0" encoding="utf-8"?>
      
      <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:fillViewport="true">
      
          <RelativeLayout
              android:id="@+id/data_entry_layout"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout_centerHorizontal="true"
              android:paddingBottom="@dimen/activity_vertical_margin"
              android:paddingLeft="@dimen/activity_horizontal_margin"
              android:paddingRight="@dimen/activity_horizontal_margin"
              android:paddingTop="@dimen/activity_vertical_margin">
              <RelativeLayout
                  android:id="@+id/Rel12345"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content">
      
              <TextView
                  android:id="@+id/post_title"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignBaseline="@+id/post_title_input"
                  android:layout_alignBottom="@+id/post_title_input"
                  android:layout_alignParentLeft="true"
                  android:layout_alignParentStart="true"
                  android:text="Post Title"
                  android:textSize="26px" />
      
              <EditText
                  android:id="@+id/post_title_input"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:layout_alignParentEnd="true"
                  android:layout_alignParentRight="true"
                  android:layout_toEndOf="@+id/post_title"
                  android:layout_toRightOf="@+id/post_title"
                  android:inputType="textCapSentences" />
      
              <TextView
                  android:id="@+id/post_description"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignParentLeft="true"
                  android:layout_alignParentStart="true"
                  android:layout_below="@+id/post_title_input"
                  android:layout_marginTop="39dp"
                  android:text="Post Description"
                  android:textSize="26px" />
      
              <EditText
                  android:id="@+id/editText"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_alignParentLeft="true"
                  android:layout_alignParentStart="true"
                  android:layout_below="@+id/post_description"
                  android:layout_marginTop="16dp"
                  android:ems="10"
                  android:inputType="textMultiLine|textCapSentences" />
      
              <ImageButton
                  android:id="@+id/imageButton1"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignParentLeft="true"
                  android:layout_alignParentStart="true"
                  android:layout_below="@+id/editText"
                  android:baselineAlignBottom="false"
                  android:contextClickable="true"
                  android:onClick="galleryIntent"
                  />
              </RelativeLayout>
      
              <RelativeLayout
                  android:id="@+id/Rel1234"
                  android:layout_below="@id/Rel12345"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignParentBottom="true">
      
                  <Button
                      android:id="@+id/cancel"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_alignParentBottom="true"
                      android:layout_alignParentLeft="true"
                      android:layout_alignParentStart="true"
                      android:text="Cancel"/>
      
                  <Button
                      android:id="@+id/submit"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_alignParentEnd="true"
                      android:layout_alignParentRight="true"
                      android:layout_alignTop="@+id/cancel"
                      android:text="Submit" />
              </RelativeLayout>
          </RelativeLayout>
      </ScrollView>
      

      并尝试将以下内容添加到您的 AndroidManifes 以删除默认键盘

      android:windowSoftInputMode="stateAlwaysHidden|adjustResize"
      

      【讨论】:

      • @cgeekmt 这是经过测试的完整解决方案
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多