2

我想以编程方式将从HTML文件中选取的表单域添加到LinearLayout。我在底部有一个下一个按钮,但它在显示屏上不断切断。我在平板电脑上试过它,但它仍然没有出现。Android ScrollView在底部被截断

这里的应用程序的截图: screenshot

正如你所看到的,这些元素越来越渲染,但最后一个逃跑出于某种原因屏幕。

片段的XML:

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context=".dataInput.PropertyInfoFragment" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:paddingLeft="20dp" 
    android:paddingRight="20dp"> 

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

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

      <LinearLayout 
       android:id="@+id/linear_layout_property_info" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical"> 

      </LinearLayout> 
      <Button 
       android:id="@+id/nextButton" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/next" 
       android:background="@color/colorPrimary" 
       android:textColor="@android:color/white"/> 
     </LinearLayout> 

    </ScrollView> 

</FrameLayout> 

呼叫活动的XML:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    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:id="@+id/main_content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context=".dataInput.DataInputActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/appbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|enterAlways" 
      app:popupTheme="@style/AppTheme.PopupOverlay"> 

     </android.support.v7.widget.Toolbar> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

    </android.support.design.widget.AppBarLayout> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="end|bottom" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@android:drawable/ic_media_play" /> 

</android.support.design.widget.CoordinatorLayout> 

我打电话的方法formInflator我在片段的onCreateView制成,从所述片段传递的LinearLayout和Elements对象(来自Jsoup库),其中包含我想要放入LinearLayout中的所有元素:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    view = inflater.inflate(R.layout.fragment_property_info, container, false); 
    nextButton = (Button) view.findViewById(R.id.nextButton); 
    nextButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      onButtonPressed(); 
     } 
    }); 

    helpers.formInflator((LinearLayout) view.findViewById(R.id.linear_layout_property_info), generator.propertyTextElements); 
    return view; 

} 

这里的方法formInflator

public void formInflator(LinearLayout parentLayout, Elements formElements) { 
    TextInputLayout index = null; 
    for(Element textField : formElements) { 
     TextInputEditText editText = new TextInputEditText(context); 
     editText.setId(View.generateViewId()); 
     editText.setHint(textField.id()); 
     editText.setText(textField.text()); 
     LinearLayout.LayoutParams editTextParams = new LinearLayout.LayoutParams(
       LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
     editText.setLayoutParams(editTextParams); 

     TextInputLayout textInputLayout = new TextInputLayout(context); 
     textInputLayout.setId(View.generateViewId()); 
     textInputLayout.setTag(textField.id()); 
     RelativeLayout.LayoutParams textInputLayoutParams = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
     if (index == null) 
      index = textInputLayout; 
     else 
      textInputLayoutParams.addRule(RelativeLayout.BELOW, index.getId()); 

     textInputLayout.setLayoutParams(textInputLayoutParams); 
     textInputLayout.addView(editText, editTextParams); 

     parentLayout.addView(textInputLayout, textInputLayoutParams); 
     index = textInputLayout; 
    } 
} 

任何想法,我做错了什么?

+0

如果它是一个滚动视图当然你可以向上滚动,看到“切客”内容的休息吗?你试图达到的目标行为是什么? – wanpanman

+0

@wanpanman我想我没有正确地说出我的问题。问题是在所有EditText视图之后应该有一个按钮。但是ScrollView不会一直滚动到底部。我在截图中显示的是最远的。 – Mak

+0

我有一种感觉,它由于操作栏。我面临同样的问题。 我正在使用标签式活动,并且在标签内部有'EditText's。最初,底部就像OP一样被切断。但是,一旦我激活一个'EditText'并且软键盘出现,操作栏就会消失(这是另一个我看起来无法解决的问题),但即使在键盘被移除后,ScrollView也能完美运行(操作栏仍然缺失)。 – xSooDx

回答

5

将您的ScrollView layout_height更改为match_parent。这是得到wrap_content的孩子。

+0

谢谢,这绝对有帮助!但它并不能完全解决问题。现在它滚动到最后一个“EditText”视图的底部,但“Button”仍然被切断。我试着移动第二个'LinearLayout'上方的'Button',导致最后一个'EditText'再次被截断 – Mak

+0

在'formInflator'中,您正在尝试为添加到'parentLayout'的'TextInputLayout'设置'RelativeLayout'布局参数'。但是'parentLayout'是一个'LinearLayout',而不是'RelativeLayout',所以不可能做你期望的。 –

3

我找到的最接近的解决方案是添加一个鬼视图...如果ScrollView吃最后一个元素的一部分,给它一个cookie。

例:

<View 
    android:layout_width="match_parent" 
    android:layout_height="25dp"/> 
0

你的动作条加入活动。当您滚动时,Android会更好地识别视图端口并在底部添加缺失的工具栏高度。

AppCompatActivity

Toolbar toolbar = getActivity().findViewById(R.id.toolbar); 
setSupportActionBar(toolbar); 
// if needed... 
getSupportActionBar().setDisplayShowTitleEnabled(false);