2016-10-22 45 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