2016-10-22 100 views
0

在添加滾動視圖之前,我的佈局在不同的設備上看起來相當不錯。我所擁有的是一行中的2張圖片,以及每張圖片右上角的複選框。但是,當我添加ScrollView時,複選框會移開,圖像出現在它們的下面。以下是目前爲止的代碼:如何使用ScrollView包裝2個線性佈局和相對佈局?

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:fadeScrollbars="false" 
      android:fillViewport="true" 
      android:orientation="vertical"> 
    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/linLay" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:orientation="vertical"> 


<!--ROW 1 --> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dip" 
      android:id="@+id/parentLayout" 
      android:layout_weight="1" 
      android:orientation="horizontal" 
      android:layout_marginTop="10dp"> 

      <RelativeLayout 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1"> 

       <ImageView 
        android:id="@+id/img1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginRight="10dp" /> 

       <CheckBox 
        android:id="@+id/checkBox1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentRight="true" 
        android:layout_alignParentTop="true" 
        android:text="" 
        android:background="@android:color/white"/> 



      </RelativeLayout> 

      <RelativeLayout 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1"> 


       <ImageView 
        android:id="@+id/img2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginRight="10dp" /> 

       <CheckBox 
        android:id="@+id/checkBox2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentRight="true" 
        android:layout_alignParentTop="true" 
        android:text="" 
        android:background="@android:color/white"/> 

      </RelativeLayout> 

     </LinearLayout> 

    <!--Row2--> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" 
     android:orientation="horizontal" 
     android:layout_marginTop="10dp"> 

     <RelativeLayout 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"> 

      <ImageView 
       android:id="@+id/img3" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="10dp" /> 

      <CheckBox 
       android:id="@+id/checkBox3" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentRight="true" 
       android:layout_alignParentTop="true" 
       android:text="" 
       android:background="@android:color/white"/> 
     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"> 

      <ImageView 
       android:id="@+id/img4" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="10dp" 
       android:layout_weight="1" /> 

      <CheckBox 
       android:id="@+id/checkBox4" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentRight="true" 
       android:layout_alignParentTop="true" 
       android:text="" 
       android:background="@android:color/white"/> 
     </RelativeLayout> 
    </LinearLayout> 
+0

scrollview標籤甚至沒有關閉 - 所以它甚至不應該編譯 – ligi

+0

只需要更多的行,在原代碼中關閉:) – user6456773

回答

0

您可以製作另一個xml,並使用include佈局標記將其包含在滾動視圖中。在android studio中創建新的滾動活動以查看相同的示例。

+0

我試圖但無法實現您的建議。你可以給我一個代碼示例嗎?謝謝! – user6456773

+0

這不是問題的答案,應該是評論而不是答案 – ligi

0

問題在於每行中的項中的weight參數。正如sanket pahuja所建議的那樣,我將文件分成兩部分,一部分與項目,另一部分與這樣的佈局。

item.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="60dp" 
    android:layout_height="wrap_content" 
    > 

    <ImageView 
     android:id="@+id/img1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="10dp" 
     android:src="@mipmap/ic_launcher"/> 

    <CheckBox 
     android:id="@+id/checkBox1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:text="" 
     /> 
</RelativeLayout> 

,另一個是layout_scroll

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fadeScrollbars="false" 
    android:fillViewport="true" 
    android:orientation="vertical"> 
    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/linLay" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:orientation="vertical"> 


     <!--ROW 1 --> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dip" 
      android:id="@+id/parentLayout" 
      android:layout_weight="1" 
      android:orientation="horizontal" 
      android:layout_marginTop="10dp"> 
      <include layout="@layout/item1"/> 
      <include layout="@layout/item1"/> 
      </LinearLayout> 
     <!--ROW 1 --> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dip" 
      android:id="@+id/parentLayout2" 
      android:layout_weight="1" 
      android:orientation="horizontal" 
      android:layout_marginTop="10dp"> 
      <include layout="@layout/item1"/> 
      <include layout="@layout/item1"/> 
     </LinearLayout> 
    </LinearLayout> 
</ScrollView> 

您可以檢查它的偉大工程。但是它將weight = 1加入到item1.xml相對佈局中,當佈局嘗試獲取整個空間時,複選框將移開。

+0

不幸的是,仍然無法正常工作。可視化是相同的 - 複選框連續出現在圖像下方。我需要箱子在圖像的右上角 – user6456773

+0

他們在我看來這樣。你使用哪個android studio版本?和sdk? – zon7