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);