2017-03-05 58 views
0

我有4个图像按钮,稍后会添加更多。我想逐一添加它们,并且每个都低于前一个。因此,我创建了一个ScrollView,以便它可以滚动查看所有按钮。但是在添加ScrollView之后,我无法移动其他元素。Android对齐图像按钮一个接一个滚动

这里是我的代码:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

<RelativeLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.exampl.mygames.MainActivity$PlaceholderFragment"> 


<ImageButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    app:srcCompat="@drawable/btntower" 
    android:id="@+id/imageButton" 
    android:layout_alignParentTop="true" 
    android:scaleType="fitXY" 
    android:layout_centerHorizontal="true" 
    android:adjustViewBounds="true" 
    android:cropToPadding="false"/> 

<ImageButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    app:srcCompat="@drawable/btntower" 
    android:id="@+id/imageButton2" 
    android:layout_alignBottom="@+id/imageButton" 
    android:scaleType="fitXY" 
    android:layout_marginTop="20dip" 
    android:layout_centerHorizontal="true" 
    android:adjustViewBounds="true" 
    android:cropToPadding="false"/> 

<ImageButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    app:srcCompat="@drawable/btntower" 
    android:id="@+id/imageButton3" 
    android:layout_alignParentTop="true" 
    android:scaleType="fitXY" 
    android:layout_centerHorizontal="true" 
    android:adjustViewBounds="true" 
    android:cropToPadding="false"/> 

<ImageButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    app:srcCompat="@drawable/btntower" 
    android:id="@+id/imageButton4" 
    android:layout_alignParentTop="true" 
    android:scaleType="fitXY" 
    android:layout_centerHorizontal="true" 
    android:adjustViewBounds="true" 
    android:cropToPadding="false"/> 
</RelativeLayout> 
</ScrollView> 
+0

如果他们毕竟相互对齐,使用的LinearLayout和'android:gravity =“center”'上的imageButtons – Zoe

回答

1

尝试以下布局:

添加android:fillViewport="true",改变android:layout_height="wrap_content"scrollview

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:fillViewport="true"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin" 
     tools:context="com.exampl.mygames.MainActivity$PlaceholderFragment"> 


     <ImageButton 
      android:id="@+id/imageButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_centerHorizontal="true" 
      android:adjustViewBounds="true" 
      android:cropToPadding="false" 
      android:scaleType="fitXY" 
      android:src="@mipmap/ic_launcher" /> 

     <ImageButton 
      android:id="@+id/imageButton2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/imageButton" 
      android:layout_centerHorizontal="true" 
      android:adjustViewBounds="true" 
      android:cropToPadding="false" 
      android:scaleType="fitXY" 
      android:src="@mipmap/ic_launcher" /> 

     <ImageButton 
      android:id="@+id/imageButton3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/imageButton2" 
      android:layout_centerHorizontal="true" 
      android:adjustViewBounds="true" 
      android:cropToPadding="false" 
      android:scaleType="fitXY" 
      android:src="@mipmap/ic_launcher" /> 

     <ImageButton 
      android:id="@+id/imageButton4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/imageButton3" 
      android:layout_centerHorizontal="true" 
      android:adjustViewBounds="true" 
      android:cropToPadding="false" 
      android:scaleType="fitXY" 
      android:src="@mipmap/ic_launcher" /> 
    </RelativeLayout> 
</ScrollView> 
+0

仍然相同,它们重叠 – androidnewbie

+0

非常感谢,它的作品没有w ^! – androidnewbie

+0

嗨,先生,但最后一个按钮不能完全显示,所以我伤口,如果你可以修复它呢? – androidnewbie