滚动视图内部的相对布局不可滚动

问题描述:

我试图制作一个UI,屏幕中间有几个字段,底部有一个按钮。当我在字段中写入数字时,键盘隐藏了界面的一部分(我希望它),但是我还需要整个界面可滚动,以便我可以调整屏幕并使用键盘填充字段上。我把整个设置放在一个相对的布局中,并放在滚动视图下。这里的帖子说它应该这样工作,但它不适合我。滚动视图内部的相对布局不可滚动

的XML代码是

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.example.maaz.rakattracker.MainActivity"> 

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 


     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_centerVertical="true" 
      android:orientation="vertical"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:orientation="horizontal"> 

       <EditText 
        android:id="@+id/farzET" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:ems="10" 
        android:gravity="center" 
        android:hint="0" 
        android:inputType="number" 
        android:singleLine="true" 
        android:textAlignment="center" /> 

       <TextView 
        android:id="@+id/farzLbl" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:elevation="0dp" 
        android:ems="5" 
        android:text="farz" 
        android:textAlignment="center" 
        android:textSize="15sp" /> 

      </LinearLayout> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:gravity="center" 
       android:orientation="horizontal"> 

       <EditText 
        android:id="@+id/sunnatET" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:ems="10" 
        android:hint="0" 
        android:inputType="number" 
        android:singleLine="true" 
        android:textAlignment="center" /> 

       <TextView 
        android:id="@+id/sunnatLbl" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:ems="5" 
        android:text="Sunnat" 
        android:textAlignment="center" 
        android:textSize="15sp" /> 
      </LinearLayout> 

      <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:gravity="center" 
       android:orientation="horizontal"> 

       <EditText 
        android:id="@+id/naflET" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:ems="10" 
        android:gravity="center" 
        android:hint="0" 
        android:inputType="number" 
        android:singleLine="true" 
        android:textAlignment="center" /> 

       <TextView 
        android:id="@+id/naflLbl" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:ems="5" 
        android:text="Nafl" 
        android:textAlignment="center" 
        android:textSize="15sp" /> 

      </LinearLayout> 

      <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:gravity="center" 
       android:orientation="horizontal"> 

       <EditText 
        android:id="@+id/witrET" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:ems="10" 
        android:hint="0" 
        android:inputType="number" 
        android:singleLine="true" 
        android:textAlignment="center" /> 

       <TextView 
        android:id="@+id/witrLbl" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:ems="5" 
        android:text="Witr" 
        android:textAlignment="center" 
        android:textSize="15sp" /> 
      </LinearLayout> 
     </LinearLayout> 

     <Button 
      android:id="@+id/startButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_centerHorizontal="true" 
      android:text="Button" /> 
    </RelativeLayout> 
</ScrollView> 

请帮我在这里找出问题。 感谢

+0

你是什么targetSdkVersion? – Thracian

尝试在你的活动添加里面的onCreate代码()方法:

getWindow()setSoftInputMode(LayoutParams.SOFT_INPUT_ADJUST_PAN)。

+0

那就是。我应该删除滚动视图,如果该模式启用? –

在您的清单中,将android:windowSoftInputMode="adjustResize"添加到您的活动标签。

在您的清单文件中添加

<activity 
     android:name=".YOURACTIVITY" 
     android:windowSoftInputMode="adjustPan" /> 
    <activity 
+0

那就是。我是否需要移除滚动视图并使其保持简单? –

,由于滚动视图只能与LinearLayout中工作

+0

我阅读了这个作品的多个职位 –

+0

没有不幸的是你不能做到这一点。 尝试把你的相对布局内线性布局,它会工作 –

+0

请参阅滚动android api: [链接](https://developer.android.com/reference/android/widget/ScrollView.html) 和一些技术见: [链接](https://material.io/guidelines/patterns/scrolling-techniques.html#scrolling-techniques-behavior) –