2017-09-16 35 views
0

我想創建一個應用程序,可以動態地將視圖添加到ConstraintLayout而不需要數據庫。我把LayoutParams包裝在這裏,看看他們要去哪裏。我已經可以添加視圖,但我不知道他們爲什麼繼續前往屏幕的左側,即使我爲每個視圖使用了不同的LayoutParams,其大小也是一樣的。利潤率似乎也不如此。下面是我的代碼:ConstraintSet.RIGHT不工作

 ConstraintSet set = new ConstraintSet(); 
     ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(
       ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT); 
     lp.setMargins(8, 8, 8, 0); 
     ConstraintLayout.LayoutParams lpWrap = new ConstraintLayout.LayoutParams(
       ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT); 
     int fieldCount = sharedPref.getInt(fieldCountTag, 0); 

     fieldCount++; 

     editor = sharedPref.edit(); 
     editor.putInt(fieldCountTag, fieldCount); 
     editor.commit(); 

     EditText txt = new EditText(ctx); 
     txt.setHint("IP:Port "+fieldCount); 
     txt.setInputType(InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT); 
     txt.setId(fieldCount); 
     txt.setLayoutParams(lp); 


     Button btn = new Button(ctx); 
     btn.setText("Use "+fieldCount); 
     btn.setId(fieldCount); 
     btn.setLayoutParams(lpWrap); 

     mainLyt.addView(txt); 
     mainLyt.addView(btn); 

     set.clone(mainLyt); 

     set.connect(btn.getId(), ConstraintSet.RIGHT, mainLyt.getId(), ConstraintSet.RIGHT, 8); 
     set.connect(btn.getId(), ConstraintSet.TOP, txt.getId(), ConstraintSet.TOP, 0); 
     set.connect(txt.getId(), ConstraintSet.LEFT, mainLyt.getId(), ConstraintSet.LEFT, 8); 
     set.connect(txt.getId(), ConstraintSet.TOP, txt3.getId(), ConstraintSet.BOTTOM, 8); 
     set.connect(txt.getId(), ConstraintSet.RIGHT, btn.getId(), ConstraintSet.LEFT, 8); 

     set.applyTo(mainLyt); 

UPDATE:

什麼即時試圖做的只是簡單地通過側添加的EditText和按鍵側

UPDATE: 我當前的代碼:

ConstraintSet set = new ConstraintSet(); 
    ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(
      ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT); 
    lp.setMargins(8, 8, 8, 0); 
    ConstraintLayout.LayoutParams lpWrap = new ConstraintLayout.LayoutParams(
      ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT); 
    int fieldCount = sharedPref.getInt(fieldCountTag, initFieldCount); 
    int topViewId = 0; 

    for(int x = 3; x < fieldCount; x++){ 
     if(txtId != 0){ 
      prevTxtId = txtId; 
     } 

     txtId = View.generateViewId(); 
     btnId = View.generateViewId(); 

     int id = x; 
     id++; 
     EditText txt = new EditText(ctx); 
     txt.setHint("IP:Port "+id); 
     txt.setInputType(InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT); 
     txt.setId(txtId); 
     txt.setLayoutParams(lp); 

     Button btn = new Button(ctx); 
     btn.setText("Use "+id); 
     btn.setId(btnId); 
     btn.setLayoutParams(lpWrap); 

     mainLyt.addView(txt); 
     mainLyt.addView(btn); 

     set.clone(mainLyt); 

     if(x == 3){ 
      Log.d(TAG, "txt3Id: "+txt3.getId()); 
      topViewId = txt3.getId(); 
     }else{ 
      topViewId = prevTxtId; 
     } 

     set.connect(btnId, ConstraintSet.END, mainLyt.getId(), ConstraintSet.END, 0); 
     set.connect(btnId, ConstraintSet.TOP, txtId, ConstraintSet.TOP, 0); 
     set.connect(txtId, ConstraintSet.START, mainLyt.getId(), ConstraintSet.START, 0); 
     set.connect(txtId, ConstraintSet.TOP, topViewId, ConstraintSet.BOTTOM, 0); 
     set.connect(txtId, ConstraintSet.END, btnId, ConstraintSet.START, 0); 

     set.applyTo(mainLyt); 
    } 

更新: 我的負載方法:

 ConstraintSet set = new ConstraintSet(); 
     ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(
       ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT); 
     lp.setMargins(8, 8, 8, 0); 
     ConstraintLayout.LayoutParams lpWrap = new ConstraintLayout.LayoutParams(
       ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT); 
     int fieldCount = sharedPref.getInt(fieldCountTag, initFieldCount); 
     int topViewId = 0; 
     Log.d(TAG, "fieldCount: "+fieldCount); 
     Log.d(TAG, "------"); 

     for(int x = 3; x < fieldCount; x++){ 
      if(txtId != 0){ 
       prevTxtId = txtId; 
      } 

      txtId = View.generateViewId(); 
      btnId = View.generateViewId(); 

      int id = x; 
      id++; 
      EditText txt = new EditText(ctx); 
      txt.setHint("IP:Port "+id); 
      txt.setInputType(InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT); 
      txt.setId(txtId); 
      txt.setLayoutParams(lp); 

      Button btn = new Button(ctx); 
      btn.setText("Use "+id); 
      btn.setId(btnId); 
      btn.setLayoutParams(lpWrap); 

      mainLyt.addView(txt); 
      mainLyt.addView(btn); 

      set.clone(mainLyt); 

      if(x == 3){ 
       Log.d(TAG, "txt3Id: "+txt3.getId()); 
       topViewId = txt3.getId(); 
      }else{ 
       topViewId = prevTxtId; 
      } 

      Log.d(TAG, "txtId: "+txtId+" connect to topView: "+topViewId+" end"+" btnId: "+btnId); 

      set.connect(btnId, ConstraintSet.END, mainLyt.getId(), ConstraintSet.END, 0); 
      set.connect(btnId, ConstraintSet.TOP, txtId, ConstraintSet.TOP, 0); 
      set.connect(txtId, ConstraintSet.START, mainLyt.getId(), ConstraintSet.START, 0); 
      set.connect(txtId, ConstraintSet.TOP, topViewId, ConstraintSet.BOTTOM, 0); 
      set.connect(txtId, ConstraintSet.END, btnId, ConstraintSet.START, 0); 

      prevTxtId = txtId; 

      set.applyTo(mainLyt); 
     } 

UPDATE:

我嘗試使用XML中3的EditText來相互連接,它能正常工作。邏輯對我的加載方法是一樣的,它仍然不起作用。意見不斷上升。日誌顯示

txtId: 1 connect to topView: 2131558527 end btnId: 2 fieldCount: 4 
txtId: 3 connect to topView: 1 end btnId: 4 fieldCount: 5 

這應該是正確的。

UPDATE:

,這裏是我的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:id="@+id/mainLyt" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="app.com.simplewebview.MainActivity"> 

    <EditText 
     android:id="@+id/txt1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="8dp" 
     android:layout_marginTop="8dp" 
     android:ems="10" 
     android:hint="IP:Port 1" 
     android:inputType="textUri" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toLeftOf="@+id/btn1" 
     app:layout_constraintTop_toTopOf="parent" /> 

    <Button 
     android:id="@+id/btn1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="8dp" 
     android:layout_marginTop="0dp" 
     android:text="Use 1" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="@+id/txt1" /> 

    <EditText 
     android:id="@+id/txt2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="8dp" 
     android:layout_marginTop="8dp" 
     android:ems="10" 
     android:hint="IP:Port 2" 
     android:inputType="textUri" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toLeftOf="@+id/btn2" 
     app:layout_constraintTop_toBottomOf="@+id/txt1" /> 

    <Button 
     android:id="@+id/btn2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="8dp" 
     android:layout_marginTop="0dp" 
     android:text="Use 2" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="@+id/txt2" /> 

    <EditText 
     android:id="@+id/txt3" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="8dp" 
     android:layout_marginTop="8dp" 
     android:ems="10" 
     android:hint="IP:Port 3" 
     android:inputType="textUri" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toLeftOf="@+id/btn3" 
     app:layout_constraintTop_toBottomOf="@+id/txt2" /> 

    <Button 
     android:id="@+id/btn3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="8dp" 
     android:layout_marginTop="0dp" 
     android:text="Use 3" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="@+id/txt3" /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="8dp" 
     android:layout_marginRight="8dp" 
     android:clickable="true" 
     app:fabSize="mini" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:srcCompat="@mipmap/ic_launcher" /> 

</android.support.constraint.ConstraintLayout> 
+0

您可能會遇到[此問題](https://stackoverflow.com/a/44200949/6287910)。 – Cheticamp

+0

所以,你的意思是它的一個錯誤 –

+0

這是一個錯誤,但我只是再看看你的代碼,我不認爲這是你的直接問題。張貼你想要做的或形容它的細節。它看起來不像你的約束設置正確。 – Cheticamp

回答

0

txtbtn需要有不同的ID或約束上是行不通的。如果您的minSdkVersion爲17或更高,請嘗試使用View.generateViewId()。無論你做什麼,你的意見都需要明確的ID。

一旦確定您設置的ID不同,請檢查您的約束。這些看起來不錯,但不清楚你在容器中的垂直約束是什麼,直到txt3有一個垂直約束。另外,如果您仍然遇到問題,請嘗試開始/結束,而不是左/右。

可能還有其他一些事情正在進行,但進行這些更改並且您應該會看到一些問題消失。

+0

它已經在工作。現在我創建了一個方法,用於在再次打開應用程序後創建回添加的視圖。你認爲我現在的代碼會起作用嗎?我創建了一個循環來重新創建它。看看我的更新 –

+0

@RickyManalo你只需要運行它看看會發生什麼。 – Cheticamp

+0

當字段已經是5時,它們會到達頂部。但是在添加它們時,它們會在前一個之下。我會發布我的加載方法 –