2017-06-03 51 views
0

我有一個約束佈局內的ImageView如下如何將9X等大小的按鈕放在約束佈局中的ImageView上?

<android.support.constraint.ConstraintLayout 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:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<ImageView 
    android:id="@+id/imageView" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="8dp" 
    android:layout_marginTop="8dp" 
    android:adjustViewBounds="true" 
    android:cropToPadding="false" 
    android:scaleType="fitXY" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintTop_toTopOf="parent" 
    app:srcCompat="@drawable/argentina" 
    android:layout_marginLeft="16dp" 
    app:layout_constraintHorizontal_bias="0.0" 
    app:layout_constraintVertical_bias="0.501" 
    android:layout_marginRight="16dp" 
    android:layout_marginStart="16dp" 
    android:layout_marginEnd="16dp" /> 

,我想蓋上9X同樣大小的按鈕,這個圖片如下所示Problem Describing Image

公告稱,ImageView的變化與約束佈局的大小

回答

1

是的你可以實現那個使用約束cha在match_constraint(0dp)財產 你要在這裏定義垂直水平到下面的所有按鈕是相同

<android.support.constraint.ConstraintLayout 
    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="match_parent" 
    tools:layout_editor_absoluteY="81dp" 
    tools:layout_editor_absoluteX="0dp"> 



     <ImageView 
      android:id="@+id/imageView" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="8dp" 
      android:layout_marginTop="8dp" 
      android:adjustViewBounds="true" 
      android:cropToPadding="false" 
      android:scaleType="fitXY" 
      app:layout_constraintBottom_toBottomOf="parent" 
      app:layout_constraintLeft_toLeftOf="parent" 
      app:layout_constraintRight_toRightOf="parent" 
      app:layout_constraintTop_toTopOf="parent" 
      app:srcCompat="@drawable/firefox" 
      android:layout_marginLeft="16dp" 
      app:layout_constraintHorizontal_bias="0.0" 
      app:layout_constraintVertical_bias="0.501" 
      android:layout_marginRight="16dp" 
      android:layout_marginStart="16dp" 
      android:layout_marginEnd="16dp" /> 
     <Button 
      android:id="@+id/button1" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:text="Button" 
      app:layout_constraintEnd_toEndOf="@+id/imageView" 
      app:layout_constraintTop_toTopOf="@+id/imageView" 
      app:layout_constraintStart_toEndOf="@+id/button5" 
      app:layout_constraintBottom_toTopOf="@id/button41" 
      app:layout_constraintVertical_chainStyle="spread_inside"/> 


     <Button 
      android:id="@+id/button31" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:text="Button" 
      app:layout_constraintStart_toStartOf="@+id/imageView" 
      app:layout_constraintTop_toTopOf="@+id/imageView" 
      app:layout_constraintBottom_toTopOf="@id/button311" 
      app:layout_constraintHorizontal_chainStyle="spread_inside" 
      app:layout_constraintEnd_toStartOf="@+id/button51" 
      app:layout_constraintVertical_chainStyle="spread_inside" /> 

     <Button 
      android:id="@+id/button5" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:text="Button" 
      app:layout_constraintTop_toTopOf="@+id/imageView" 
      app:layout_constraintEnd_toStartOf="@+id/button11" 
      app:layout_constraintStart_toEndOf="@+id/button13" 
      app:layout_constraintBottom_toTopOf="@+id/button133" 
      app:layout_constraintVertical_chainStyle="spread_inside" /> 




     <Button 
      android:id="@+id/button311" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:text="Button" 
      app:layout_constraintStart_toStartOf="@+id/imageView" 
      app:layout_constraintTop_toBottomOf="@+id/button31" 
      app:layout_constraintBottom_toTopOf="@+id/button13" 
      app:layout_constraintEnd_toStartOf="@+id/button133" 
      app:layout_constraintHorizontal_chainStyle="spread_inside" /> 

     <Button 
      android:id="@+id/button133" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:text="Button" 
      app:layout_constraintStart_toEndOf="@+id/button311" 
      app:layout_constraintEnd_toStartOf="@+id/button41" 
      app:layout_constraintTop_toBottomOf="@+id/button5" 
      app:layout_constraintBottom_toTopOf="@+id/button51" /> 
     <Button 
      android:id="@+id/button41" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      app:layout_constraintEnd_toEndOf="@+id/imageView" 
      android:text="Button" 
      app:layout_constraintTop_toBottomOf="@+id/button1" 
      app:layout_constraintBottom_toTopOf="@+id/button11" 
      app:layout_constraintStart_toEndOf="@+id/button133" /> 

     <Button 
      android:id="@+id/button11" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:text="Button" 
      app:layout_constraintEnd_toEndOf="@+id/imageView" 
      app:layout_constraintBottom_toBottomOf="@+id/imageView" 
      app:layout_constraintStart_toEndOf="@+id/button51" 
      app:layout_constraintTop_toBottomOf="@id/button41" 
      /> 


     <Button 
      android:id="@+id/button13" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:text="Button" 
      app:layout_constraintStart_toStartOf="@+id/imageView" 
      app:layout_constraintBottom_toBottomOf="@+id/imageView" 
      app:layout_constraintTop_toBottomOf="@id/button311" 
      app:layout_constraintHorizontal_chainStyle="spread_inside" 
      app:layout_constraintEnd_toStartOf="@+id/button51" /> 

     <Button 
      android:id="@+id/button51" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:text="Button" 
      app:layout_constraintBottom_toBottomOf="@+id/imageView" 
      app:layout_constraintEnd_toStartOf="@+id/button11" 
      app:layout_constraintStart_toEndOf="@+id/button13" 
      app:layout_constraintTop_toBottomOf="@id/button133"/> 
</android.support.constraint.ConstraintLayout> 

這裏的樣本是設計 and 藍色印花 design and blue print

,你可以在這裏得到有關鏈和約束所有的細節 https://developer.android.com/reference/android/support/constraint/ConstraintLayout.html#Chains

這將在所有設備大小和方向的工作

注:我建議吳太添加一些了minHeight到圖像視圖,以便避免隱藏按鈕,但我不知道你的要求,但它是一個引人注目的點

+0

非常感謝這就是我正在尋找一個更多的問題:有沒有什麼辦法來消除按鈕之間的空白(垂直和水平) ! –

+0

間距是默認設置,對於線性佈局也是如此,但是當你設置按鈕背景時,它將覆蓋與背景的間距,如果你在這裏覆蓋imageview,那麼爲什麼你在背景中使用它。 – Pavan

+0

這些按鈕將在以後不可見! –