2017-07-08 101 views
0

我正處於Android開發階段。最近,爲了練習佈局設計,我試圖建立一個簡單計算器的模擬。我在「設計」選項卡中創建並放置了視圖(按鈕,EditTexts)。然後我被困在「這個視圖不受約束」的錯誤,所以我搜索了StackOverflow,最後發現解決方案是右鍵單擊小部件 - >約束佈局 - >推斷約束。但是當我做到這一點時,我的程序中的這些視圖發生了很大的變化,我無法將它們修改爲原始位置和大小。如何在不改變視圖的位置和大小的情況下解決錯誤?在Android Studio中使用約束佈局會造成奇怪的後果

這是之前使用約束佈局我的計算器:

before_constraint:

enter image description here

這是使用後,我的計算器吧:

after_constraint:

enter image description here

這是我的源代碼:

<?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" 
tools:context="com.huy9515gmail.helloworld.MainActivity" 
tools:layout_editor_absoluteY="81dp" 
tools:layout_editor_absoluteX="0dp"> 


<Button 
    android:id="@+id/button10" 
    android:layout_width="53dp" 
    android:layout_height="45dp" 
    android:text="0" 
    tools:layout_editor_absoluteX="87dp" 
    tools:layout_editor_absoluteY="186dp" /> 

<Button 
    android:id="@+id/button7" 
    android:layout_width="53dp" 
    android:layout_height="45dp" 
    android:text="7" 
    tools:layout_editor_absoluteX="34dp" 
    tools:layout_editor_absoluteY="141dp" /> 

<Button 
    android:id="@+id/button8" 
    android:layout_width="53dp" 
    android:layout_height="45dp" 
    android:text="8" 
    tools:layout_editor_absoluteX="87dp" 
    tools:layout_editor_absoluteY="141dp" /> 

<Button 
    android:id="@+id/button9" 
    android:layout_width="53dp" 
    android:layout_height="45dp" 
    android:text="9" 
    tools:layout_editor_absoluteX="140dp" 
    tools:layout_editor_absoluteY="141dp" /> 

<TextView 
    android:id="@+id/textView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="My First Calculator" 
    tools:layout_editor_absoluteX="134dp" 
    tools:layout_editor_absoluteY="16dp" /> 

<Button 
    android:id="@+id/button" 
    android:layout_width="53dp" 
    android:layout_height="45dp" 
    android:text="2" 
    tools:layout_editor_absoluteX="87dp" 
    tools:layout_editor_absoluteY="51dp" /> 

<Button 
    android:id="@+id/button3" 
    android:layout_width="53dp" 
    android:layout_height="45dp" 
    android:text="3" 
    tools:layout_editor_absoluteX="140dp" 
    tools:layout_editor_absoluteY="51dp" /> 

<Button 
    android:id="@+id/button4" 
    android:layout_width="53dp" 
    android:layout_height="45dp" 
    android:text="/" 
    tools:layout_editor_absoluteX="281dp" 
    tools:layout_editor_absoluteY="96dp" /> 

<Button 
    android:id="@+id/button14" 
    android:layout_width="53dp" 
    android:layout_height="45dp" 
    android:text="x" 
    tools:layout_editor_absoluteX="228dp" 
    tools:layout_editor_absoluteY="96dp" /> 

<Button 
    android:id="@+id/button13" 
    android:layout_width="53dp" 
    android:layout_height="45dp" 
    android:text="-" 
    tools:layout_editor_absoluteX="281dp" 
    tools:layout_editor_absoluteY="51dp" /> 

<Button 
    android:id="@+id/button12" 
    android:layout_width="53dp" 
    android:layout_height="45dp" 
    android:text="+" 
    tools:layout_editor_absoluteX="228dp" 
    tools:layout_editor_absoluteY="51dp" /> 

<Button 
    android:id="@+id/button11" 
    android:layout_width="106dp" 
    android:layout_height="40dp" 
    android:text="Delete" 
    tools:layout_editor_absoluteX="228dp" 
    tools:layout_editor_absoluteY="210dp" /> 

<Button 
    android:id="@+id/button16" 
    android:layout_width="106dp" 
    android:layout_height="40dp" 
    android:text="Calculate" 
    tools:layout_editor_absoluteX="228dp" 
    tools:layout_editor_absoluteY="170dp" /> 

<Button 
    android:id="@+id/button15" 
    android:layout_width="53dp" 
    android:layout_height="45dp" 
    android:text="1" 
    tools:layout_editor_absoluteX="34dp" 
    tools:layout_editor_absoluteY="51dp" /> 

<Button 
    android:id="@+id/button2" 
    android:layout_width="53dp" 
    android:layout_height="45dp" 
    android:text="4" 
    tools:layout_editor_absoluteX="34dp" 
    tools:layout_editor_absoluteY="96dp" /> 

<Button 
    android:id="@+id/button5" 
    android:layout_width="53dp" 
    android:layout_height="45dp" 
    android:text="5" 
    tools:layout_editor_absoluteX="87dp" 
    tools:layout_editor_absoluteY="96dp" /> 

<Button 
    android:id="@+id/button6" 
    android:layout_width="53dp" 
    android:layout_height="45dp" 
    android:text="6" 
    tools:layout_editor_absoluteX="140dp" 
    tools:layout_editor_absoluteY="96dp" /> 

<EditText 
    android:id="@+id/editText" 
    android:layout_width="310dp" 
    android:layout_height="98dp" 
    android:ems="10" 
    android:inputType="textPersonName" 
    tools:layout_editor_absoluteX="37dp" 
    tools:layout_editor_absoluteY="327dp" /> 

+0

爲什麼使用硬編碼大小? –

+0

硬編碼尺寸?對不起,但....我不明白@@他們是什麼? –

+0

您已明確設置爲「## dp」的任何內容。理想情況下,數字鍵盤被包裝在GridLayout中。每個按鈕都可以擴展到網格單元的大小。您將網格相對於整個屏幕(具有水平LinearLayout)以及該LinearLayout中的大小進行調整,然後將其他按鈕放置在另一個單獨的垂直LinearLayout中,方法與此類似。您希望按鈕都是一個單位,是我的觀點 –

回答

0

當您「推斷約束」時,硬編碼值就出現了。我建議你刪除所有佈局並重新開始,瞭解約束佈局如何工作。如果您看到「此視圖不受約束」錯誤,Android Studio告訴您的是:視圖需要更多約束

請記住iOS上的AutoLayout和Android上的ConstraintLayout的核心規則:每個視圖都需要知道自己的位置(水平和垂直),同時也需要知道它的尺寸,寬度和高度。

所以,想想你的佈局......我給你舉個例子。

從頂部開始,您希望「標籤」以文本「歡迎使用我的計算器」爲中心。

好,現在發動機需要知道的4件事:

  1. 凡在X軸我把這個標籤?
  2. 我在Y軸上的位置放了這個標籤嗎?
  3. 這個標籤的寬度是多少?
  4. 這個標籤的高度是多少?

有一些方法可以讓發動機推斷其中的一些(我們將會看到),但在大多數情況下,你需要這四個,按次(無論他們是否是推斷或明確)。

那麼讓我們從您的「標題」開始。

TextView將居中,但我確定您希望它跨越它可以找到的所有寬度(並在需要時進行壓縮)。由於ConstraintLayout沒有match_parent(出於顯而易見的原因:又名:想法是你必須設置約束),我們必須能夠以不同的方式做...

對於X軸(水平),我們希望textView被固定到parent(又名:本例中的根)的start,以及父項的end(有效地使用所有屏幕,減去您決定在任一側添加的任何邊距)。

到目前爲止,有兩個約束條件。對於上述工作,ConstraintLayout說你必須在width中使用「特殊」值0dp。這告訴引擎寬度將是「任何我還不知道的值,在計算完所有約束條件後我會知道的」。

好。現在我們已經解決了#1和#3。

對於#2和#4,你可以做類似的事情......或者你可以享受一些事實,即某些觀點,推斷出了價值。例如:文本視圖不需要高度,它們將根據字體大小,字符數量等自動調整大小,但仍需要告訴它該文本視圖將在哪個軸上進行。

就你而言,我只是將文本視圖的top與你的parent.top對齊。注意,您不需要告訴佈局將文字的bottom固定爲任何東西,因爲它的高度= wrap_content

,...等

現在,使用the official docs並開始閱讀更多您可以做的事情(鏈,例如,可以在這裏派上用場)。

0

你的意見都沒有限制,但它們都具有設計時的位置。關於運行時間約束,請看看這個documentationThis也是一個很好的介紹。

設計時位置看起來像這樣的事情:

tools:layout_editor_absoluteY="81dp" 
tools:layout_editor_absoluteX="0dp" 

此代碼將定位在設計視圖。運行時間限制是這樣的:

app:layout_constraintBottom_toBottomOf=」@+id/constraintLayout」 
app:layout_constraintEnd_toStartOf=」@+id/imageView」 
app:layout_constraintStart_toStartOf=」@+id/constraintLayout」 

,並在運行時定位您的視圖(和設計師)

不幸的是,當你推斷的約束一些不好的事情發生了。設計師做出了最好的猜測,但並不是想要的。

我建議您按照視圖進行佈局,刪除設計時約束並添加運行時間約束,直到佈局看起來像你想要的。