2017-08-30 33 views
0

我試圖讓我的按鈕均勻填充空間,基本上每個按鈕填充剩餘空間的三分之一(有三個按鈕)。用重量創建靈活的按鈕(鏈接+傳播)

根據here,我應該鏈接的意見(按鈕),然後傳播他們和權重,如果有必要填補空間。然後,我應該將按鈕垂直約束更改爲「匹配約束」。然而,這會導致按鈕不平坦。我終於在XML文件的文本中添加了layout_constraintVertical_weight=""到各個button,但無濟於事。使用android:layout_height也不起作用。

的XML代碼:

<?xml version="1.0" encoding="utf-8"?> 
<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" 
    android:background="#CCFFE5" 
    android:weightSum="1" 
    tools:context="com.example.shaynimex.boomnumber.HomeScreen"> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_height="52dp" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="8dp" 
     android:layout_marginTop="16dp" 
     android:fontFamily="cursive" 
     android:text="Boom Number" 
     android:textAlignment="center" 
     android:textSize="50sp" 
     android:textStyle="bold" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     android:id="@+id/textView" 
     android:layout_marginStart="8dp" 
     android:layout_marginEnd="8dp" /> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="1 PLAYER" 
     android:textSize="30sp" 
     android:textStyle="bold" 
     app:layout_constraintBottom_toTopOf="@+id/button3" 
     app:layout_constraintHorizontal_bias="0.0" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintVertical_chainStyle="packed" 
     android:layout_marginTop="8dp" 
     app:layout_constraintTop_toBottomOf="@+id/textView" /> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="2 PLAYER" 
     android:textAlignment="center" 
     android:textSize="30sp" 
     android:textStyle="bold" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintHorizontal_bias="1.0" 
     android:layout_marginTop="8dp" 
     app:layout_constraintTop_toBottomOf="@+id/button" 
     app:layout_constraintBottom_toTopOf="@+id/button4" /> 

    <Button 
     android:id="@+id/button4" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="How to Play" 
     android:textAlignment="center" 
     android:textSize="30sp" 
     android:textStyle="bold" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintHorizontal_bias="0.0" 
     app:layout_constraintTop_toBottomOf="@+id/button3" 
     app:layout_constraintBottom_toBottomOf="parent" /> 

這也是目前看起來像這樣(爲像素XL):

enter image description here

那麼我將如何去應用的權重到按鈕,並使他們均勻地填充剩餘的空間?

+0

對不使用ConstraintLayout感到抱歉。謝謝你接受我的回答。 –

回答

0

由於權重的困難,我沒有使用ConstraintLayout。

enter image description here

如果這就是你想要什麼,我只是做了這種代碼。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#CCFFE5"> 

    <TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:fontFamily="cursive" 
    android:text="Boom Number" 
    android:textAlignment="center" 
    android:textSize="50sp" 
    android:textStyle="bold" 
    android:id="@+id/textView"/> 


    <FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="1 PLAYER" 
     android:textSize="30sp" 
     android:textStyle="bold"/> 
    </FrameLayout> 

    <FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="2 PLAYER" 
     android:textAlignment="center" 
     android:textSize="30sp" 
     android:textStyle="bold" 
     android:layout_marginTop="8dp"  /> 
    </FrameLayout> 

    <FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"> 

    <Button 
     android:id="@+id/button4" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="How to Play" 
     android:textAlignment="center" 
     android:textSize="30sp" 
     android:textStyle="bold"/> 
    </FrameLayout> 
    </LinearLayout> 
+1

它解決了我的問題。謝謝! –

+0

不客氣。 –