2014-10-06 68 views
0

我有一個包含一個子佈局(線性佈局)的滾動視圖,並且該子包含相對佈局和線性佈局。相對佈局和線性佈局共享半屏。線性佈局具有動態添加的按鈕。ScrollView不滾動溢出佈局

現在問題出現了,因爲滾動視圖在滾動相對佈局和線性佈局時不滾動按鈕。

<ScrollView> 
<LinearLayout> 

    <RelativeLayout>  
    </RelativeLayout> 

    <LinearLayout> 
          ->Dynamically adding buttons here 
    </LinearLayout> 

</LinearLayout> 
</ScrollView> 

我想這些按鈕與滾動視圖滾動

僅供參考我的XML代碼: -

<LinearLayout 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" 
android:background="@color/whitecolor" 
android:orientation="vertical" 
> 

<ScrollView 
    android:id="@+id/scrollview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:fillViewport="true" 
    > 

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

<RelativeLayout 
    android: 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="vertical" 
    > 
<View 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/bluecolor" 
    /> 
<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/stauslinearlayout" 
    android:layout_marginBottom="8dp" 
    android:padding="5dp" 
    android:gravity="center" 
    android:textColor="@color/whitecolor" 
    android:text="@string/app_name" 
    /> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_alignParentBottom="true" 
    android:background="@color/bluecolor" 
    > 
<EditText 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:background="@drawable/edit_text_custom" 
    android:singleLine="true" 
    android:hint="@string/status" 
    android:textColor="#000" 
    android:textColorHint="#5222" 
    android:padding="7dp" 
    android:maxLength="50" 
    android:cursorVisible="true" 
    android:layout_marginBottom="10dp" 
    android:layout_marginLeft="10dp" 
    android:drawableLeft="@android:drawable/ic_input_add" 
    android:ems="10" 
    /> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="right|center_horizontal" 
    android:gravity="center" 
    android:layout_weight="0" 
    android:layout_marginBottom="10dp" 
    android:layout_marginRight="10dp" 
    android:padding="5dp" 
    android:text="@string/statusnumber" 
    android:background="@drawable/edit_text_custom" 
    /> 
</LinearLayout> 
</RelativeLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:orientation="vertical" 
    > 
<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/whitecolor" 
    android:text="abcdfrg" 
    /> 
<Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/app_name" 
    /> 
<Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/app_name" 
    /> 
    <Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/app_name" 
    /> 
    <Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/app_name" 
    /> 
     <Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/app_name" 
    /> 
     <Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/app_name" 
    />    
</LinearLayout> 
</LinearLayout> 
</ScrollView> 

回答

0

據我所知,如果你想在以同樣的分屏兩種尺寸,您必須在底部視圖中採用另一個scrollView,如下面的示例...

<LinearLayout 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" 
    android:orientation="vertical" > 

    <ScrollView 
     android:id="@+id/scrollview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:fillViewport="true" > 

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

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="1" 
       android:orientation="vertical" > 

       <View 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="@android:color/darker_gray" /> 

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_above="@+id/stauslinearlayout" 
        android:layout_marginBottom="8dp" 
        android:gravity="center" 
        android:padding="5dp" 
        android:text="@string/app_name" 
        android:textColor="@android:color/white" /> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true" 
        android:background="@android:color/darker_gray" 
        android:orientation="horizontal" > 

        <EditText 
         android:layout_width="0dp" 
         android:layout_height="wrap_content" 
         android:layout_marginBottom="10dp" 
         android:layout_marginLeft="10dp" 
         android:layout_weight="1" 
         android:cursorVisible="true" 
         android:drawableLeft="@android:drawable/ic_input_add" 
         android:ems="10" 
         android:hint="status" 
         android:maxLength="50" 
         android:padding="7dp" 
         android:singleLine="true" 
         android:textColor="#000" 
         android:textColorHint="#5222" /> 

        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="match_parent" 
         android:layout_gravity="right|center_horizontal" 
         android:layout_marginBottom="10dp" 
         android:layout_marginRight="10dp" 
         android:layout_weight="0" 
         android:gravity="center" 
         android:padding="5dp" 
         android:text="statusnumber" /> 
       </LinearLayout> 
      </RelativeLayout> 

      <ScrollView 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="1" > 

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

        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:background="@android:color/white" 
         android:text="abcdfrg" /> 

        <Button 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="@string/app_name" /> 

        <Button 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="@string/app_name" /> 

        <Button 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="@string/app_name" /> 

        <Button 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="@string/app_name" /> 

        <Button 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="@string/app_name" /> 

        <Button 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="@string/app_name" /> 
       </LinearLayout> 
      </ScrollView> 
     </LinearLayout> 
    </ScrollView> 

</LinearLayout> 

如果你不想採取另一個scrollView,那麼你必須給wrap_contant底部線性佈局。

<LinearLayout 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" 
    android:orientation="vertical" > 

    <ScrollView 
     android:id="@+id/scrollview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:fillViewport="true" > 

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

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="1" 
       android:orientation="vertical" > 

       <View 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="@android:color/darker_gray" /> 

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_above="@+id/stauslinearlayout" 
        android:layout_marginBottom="8dp" 
        android:gravity="center" 
        android:padding="5dp" 
        android:text="@string/app_name" 
        android:textColor="@android:color/white" /> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true" 
        android:background="@android:color/darker_gray" 
        android:orientation="horizontal" > 

        <EditText 
         android:layout_width="0dp" 
         android:layout_height="wrap_content" 
         android:layout_marginBottom="10dp" 
         android:layout_marginLeft="10dp" 
         android:layout_weight="1" 
         android:cursorVisible="true" 
         android:drawableLeft="@android:drawable/ic_input_add" 
         android:ems="10" 
         android:hint="status" 
         android:maxLength="50" 
         android:padding="7dp" 
         android:singleLine="true" 
         android:textColor="#000" 
         android:textColorHint="#5222" /> 

        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="match_parent" 
         android:layout_gravity="right|center_horizontal" 
         android:layout_marginBottom="10dp" 
         android:layout_marginRight="10dp" 
         android:layout_weight="0" 
         android:gravity="center" 
         android:padding="5dp" 
         android:text="statusnumber" /> 
       </LinearLayout> 
      </RelativeLayout> 

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

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="@android:color/white" 
        android:text="abcdfrg" /> 

       <Button 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/app_name" /> 

       <Button 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/app_name" /> 

       <Button 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/app_name" /> 

       <Button 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/app_name" /> 

       <Button 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/app_name" /> 

       <Button 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/app_name" /> 
      </LinearLayout> 
     </LinearLayout> 
    </ScrollView> 

</LinearLayout> 

選擇滿足您的要求。

如果有問題呢,然後PLZ問... :-)

+0

我不能,因爲我希望兩個孩子佈局與單滾動視圖滾動使用第一種方法。第二種方法與我的問題具有相同的行爲。不過,我設法通過動態設置第一個孩子身高到屏幕高度/ 2,然後動態地添加按鈕到滾動視圖。非常感謝! – Aviverma 2014-10-08 04:24:43

+0

@Aviverma,好的歡迎。 – Rumit 2014-10-08 04:28:48