2015-08-14 57 views
1

的框我一直在試圖弄清楚如何放置一個容器,該容器在下面的內容的ScrollView中垂直填充整個屏幕。Android:填充整個屏幕的內容低於

事情是這樣的:

  • [BOX填充整個屏幕]
  • [一些其他的東西,/需要滾動查看]

這是內容我想下面盒子: http://myupload.dk/handleupload/ce34836ostt

我試過了一切 - 看起來像我ca不是將絕對位置填充到填充整個屏幕的容器。我可以製作一個填滿整個屏幕的盒子,但是我在它下面添加的所有東西都會將盒子擠在一起。

它工作時,我的尺寸添加到框,就像這樣:

<com.github.ksoichiro.android.observablescrollview.ObservableScrollView 

    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal|top" 
    android:minWidth="25px" 
    android:minHeight="0px" 
    android:paddingTop="0dp" 
    android:fillViewport="true" 
    android:id="@+id/scroll"> 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

     <!-- THIS BOX I WOULD LIKE TO FIT THE ENTIRE SCREEN --> 

     <android.support.v4.view.ViewPager 
      android:layout_width="match_parent" 
      android:layout_height="100dp" 
      android:id="@+id/cardsPager" /> 

     <!-- THIS CONTENT SHOULD BE BELOW THE BOX ABOVE --> 

     <fragment android:name="bonnier.hvadsynes.fragments.DetailsFragment" 
        android:id="@+id/details_fragment" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"/> 


    </LinearLayout> 
</com.github.ksoichiro.android.observablescrollview.ObservableScrollView> 

非常感謝您的幫助!

  • 西蒙

回答

2

我的想法是通過編程設置屏幕填充盒的高度。

何時何地:取決於您使用的內容,例如onResume當然有更好的地方,但現在它的工作。 如何:將OnGlobalLayoutListener添加到您的viewtreeobserver,然後將您的scrollviews高度複製到「框」。

<!--Whatever the whole things height should be, e.g. match_parent or 100dp or or or--> 
<ScrollView 
    android:background="#0f0" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/scrollview"> 

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

     <!-- THIS BOX I WOULD LIKE TO FIT THE ENTIRE SCREEN --> 
     <!-- Any height works as we overwrite it anyway--> 
     <View 
      android:id="@+id/makebig" 
      android:background="#f00" 
      android:layout_width="match_parent" 
      android:layout_height="300dp" /> 

     <!-- THIS CONTENT SHOULD BE BELOW THE BOX ABOVE --> 
     <View 
      android:background="#0ff" 
      android:id="@+id/details_fragment" 
      android:layout_width="match_parent" 
      android:layout_height="100dp"/> 

    </LinearLayout> 
</ScrollView> 

在您的活動或任何地方:例如在的onResume()

final View makebig = findViewById(R.id.makebig); 
final View scrollview = findViewById(R.id.scrollview); 

final ViewTreeObserver viewTreeObserver = scrollview.getViewTreeObserver(); 
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
     //only do this all once 
     scrollview.getViewTreeObserver().removeOnGlobalLayoutListener(this); 

     makebig.getLayoutParams().height = scrollview.getHeight(); 
    } 
}); 
+0

真的很好,乾淨的方式來做到這一點,也明白你用我的模板的例子 - 感謝分享,並花時間迴應... –

2

你可以這樣來做:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

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

    <LinearLayout 
     android:id="@+id/layout1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#A5CB3A" 
     android:orientation="vertical" > 


    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/layout2" 
     android:layout_width="match_parent" 
     android:layout_height="200dp" 
     android:orientation="vertical" > 

     <!-- here goes your textview --> 

    </LinearLayout> 
</LinearLayout> 

而且在活動

Point size = new Point(); 
    getWindowManager().getDefaultDisplay().getSize(size); 
    int screenHeight = size.y; 

    LinearLayout layout = (LinearLayout)findViewById(R.id.layout1); 

    ViewGroup.LayoutParams params = layout.getLayoutParams(); 
    params.height = screenHeight; //here you can substract the height of the top bar, if your app has one 

    layout.setLayoutParams(params); 

的onCreate方法你只需設置高度屏幕的內部佈局。