2015-12-08 61 views
-1

我有一個滾動視圖,裏面是線性佈局,並且在線性佈局裏面有一些按鈕。我爲線性佈局定義了一些填充,但在滾動時,按鈕會忽略它並與滾動視圖重疊。該怎麼辦? 這裏是XML:Android,保持按鈕忽略父補間?

<ScrollView 
    android:id="@+id/scrollView2" 
    android:layout_width="0dp" 
    android:layout_height="140dp" 
    android:layout_weight="1" 
    android:background="@drawable/item" 
    android:fadeScrollbars="false" 
    android:fadingEdge="none"> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="8dp" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="8dp" 
     android:layout_marginTop="8dp" 
     android:background="@drawable/item" 
     android:gravity="center" 
     android:orientation="vertical"> 

     <Button 
      android:id="@+id/Button03" 
      android:layout_width="fill_parent" 
      android:layout_height="50dp" 
      android:background="#ff0000" 
      android:text="Button" /> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="1dp" 
      android:background="#1e90ff" /> 

     <Button 
      android:id="@+id/Button02" 
      android:layout_width="fill_parent" 
      android:layout_height="50dp" 
      android:background="#ff0000" 
      android:text="Button" /> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="1dp" 
      android:background="#1e90ff" /> 

     <Button 
      android:id="@+id/Button01" 
      android:layout_width="fill_parent" 
      android:layout_height="50dp" 
      android:background="#ff0000" 
      android:text="Button" /> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="1dp" 
      android:background="#1e90ff" /> 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="fill_parent" 
      android:layout_height="50dp" 
      android:background="#ff0000" 
      android:text="Button" /> 
    </LinearLayout> 
</ScrollView> 

回答

0

這是因爲填充空間是滾動視圖的一部分。線性佈局在滾動視圖內,填充空間在線性佈局內,滾動視圖內的任何內容都將滾動。所以,填充的空間也會滾動。

如果要保留填充空間,請添加填充空間以滾動查看。

代碼:

<ScrollView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/scrollView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="8dp" 
     android:layout_marginTop="8dp" 
     android:layout_marginBottom="8dp" 
     android:fadeScrollbars="false" 
     android:fadingEdge="none" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:background="@mipmap/ic_launcher" 
      android:orientation="vertical"> 

      <Button 
       android:id="@+id/Button03" 
       android:layout_width="fill_parent" 
       android:layout_height="250dp" 
       android:background="#ff0000" 
       android:text="Button" /> 
      <View 
       android:layout_width="match_parent" 
       android:layout_height="1dp" 
       android:background="#1e90ff"/> 

      <Button 
       android:id="@+id/Button02" 
       android:layout_width="fill_parent" 
       android:layout_height="250dp" 
       android:background="#ff0000" 
       android:text="Button" /> 
      <View 
       android:layout_width="match_parent" 
       android:layout_height="1dp" 
       android:background="#1e90ff"/> 

      <Button 
       android:id="@+id/Button01" 
       android:layout_width="fill_parent" 
       android:layout_height="250dp" 
       android:background="#ff0000" 
       android:text="Button" /> 
      <View 
       android:layout_width="match_parent" 
       android:layout_height="1dp" 
       android:background="#1e90ff"/> 

      <Button 
       android:id="@+id/button1" 
       android:layout_width="fill_parent" 
       android:layout_height="250dp" 
       android:background="#ff0000" 
       android:text="Button" /> 

     </LinearLayout> 
    </ScrollView> 

現在,填充空間將不會通過按鈕覆蓋。 希望它能解決您的問題。

+0

非常有用。使用滾動視圖中的所有內容滾動的這一觀點,我設法將滾動視圖設置爲線性佈局,並將填充設置爲滾動視圖本身而不是其子視圖。然後我做了線性佈局的角落圓角,現在一切都聽起來不錯。感謝Abhinav Puri。 –