【问题标题】:Android relative layout inside scrollview not scrolling in landscape mode滚动视图内的Android相对布局不在横向模式下滚动
【发布时间】:2015-12-29 00:24:44
【问题描述】:

您好,我已经将我所有的 UI 元素放在一个相对布局中,然后将相对布局放在滚动视图中以实现屏幕滚动。但是布局没有在横向模式下滚动。我在这里错过了什么。有什么想法吗?

谢谢, P

更新:视图也不会在肖像模式下滚动。我添加了一些额外的 UI 元素,视图也不会在此模式下滚动。我正在使用 Nexus 6P 手机进行测试。

这里是xml

<ScrollView 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"
    tools:context="layout.LoginFragment"
    android:fillViewport="true"
  >


 <RelativeLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent">
    <ImageView
        android:layout_width="92dp"
        android:layout_height="92dp"
        android:id="@+id/imageView"
        android:layout_marginLeft="37dp"
        android:layout_marginStart="37dp"
        android:src="@mipmap/ic_logo"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="41dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/login_hello"
        android:id="@+id/login_tv1"

        android:fontFamily="Roboto"
        android:textColor="#000000"
        android:textStyle="bold"
        android:textSize="20dp"

        android:layout_alignTop="@+id/imageView"
        android:layout_toRightOf="@+id/imageView"
        android:layout_toEndOf="@+id/imageView"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="25dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="@string/login_text"
        android:id="@+id/login_tv2"
        android:fontFamily="Roboto"
        android:textColor="#000000"
        android:textSize="16dp"
        android:layout_marginTop="25dp"
        android:layout_alignBottom="@+id/imageView"
        android:layout_alignLeft="@+id/login_tv1"
        android:layout_alignTop="@+id/login_tv1" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:ems="10"
        android:id="@+id/username_et"
        android:fontFamily="Roboto"
        android:layout_centerVertical="true"
        android:hint="@string/login_username"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:textColor="#000000"
        android:layout_alignLeft="@+id/imageView"
        android:layout_alignStart="@+id/imageView"
        android:textColorHint="#000000"
        android:textSize="18dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:width="50dp"
        android:ems="10"
        android:id="@+id/password_et"
        android:layout_below="@+id/username_et"
        android:layout_alignLeft="@+id/username_et"
        android:layout_alignStart="@+id/username_et"
        android:layout_marginTop="33dp"
        android:layout_alignRight="@+id/username_et"
        android:layout_alignEnd="@+id/username_et"
        android:password="true"
        android:hint="@string/login_password"
        android:fontFamily="Roboto"
        android:textColorHint="#000000"
        android:textSize="18dp" />

    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/rememberme_switch"
        android:layout_below="@+id/password_et"
        android:layout_alignLeft="@+id/password_et"
        android:layout_alignStart="@+id/password_et"
        android:layout_marginTop="53dp"
        android:layout_alignRight="@+id/password_et"
        android:layout_alignEnd="@+id/password_et"
        android:hint="@string/remember_me"
        android:textOff="@string/off"
        android:textOn="@string/on"
        android:textSize="18dp"
        android:clickable="true"
        android:checked="false"
        android:showText="true"
        android:enabled="true"
        android:focusable="true"
        android:trackTint="#000000"
        android:textColorLink="#000000"
        android:textColorHint="#000000" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/help"
        android:id="@+id/help_button"
        android:background="@android:color/transparent"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_below="@+id/rememberme_switch"
        android:layout_alignStart="@+id/rememberme_switch"
        android:layout_marginTop="34dp"
        android:textSize="18dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/login"
        android:id="@+id/login_button"
        android:layout_marginEnd="32dp"
        android:background="@android:color/transparent"
        style="?android:attr/borderlessButtonStyle"
        android:layout_alignTop="@+id/help_button"
        android:layout_alignEnd="@+id/rememberme_switch"
        android:textSize="18dp" />

     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:id="@+id/error_tv"
         android:textColor="#f92626"
         android:fontFamily="Roboto"
         android:textSize="16dp"
         android:layout_below="@+id/password_et"
         android:layout_alignStart="@+id/password_et"
         android:layout_alignEnd="@+id/login_button" />

 </RelativeLayout>
</ScrollView>`

【问题讨论】:

  • RelativeLayout 宽度/高度更改为wrap_content 会发生什么?
  • 您好,您是否尝试将 RelativeLayout 放入 LinearLayout 看起来像:ScrollView -> LinearLayout -> RelativeLayout ->你的内容

标签: android scrollview android-relativelayout


【解决方案1】:

我刚刚测试了一个类似的布局,它的滚动效果很好。滚动视图内部的内容需要大于滚动条显示的屏幕尺寸。或者您可以为滚动视图设置一个大小,然后只要里面的内容较大,它就会滚动。

<ScrollView 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"
    tools:context="layout.LoginFragment"
    android:fillViewport="true"
  >
 <RelativeLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent">
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add a lot of text here so the text will fill page and scroll aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
            android:textSize="20dp" />

 </RelativeLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-13
    • 1970-01-01
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    相关资源
    最近更新 更多