0

Ich habe eine Frage, was ich tun möchte, ist ConstraintLayout zu verwenden, die ich habe (alle seine Eigenschaften auch) ein Fragment in der Ausführungszeit hinzuzufügen und zu positionieren.Verwenden ConstraintLayout Fragmentposition zu verwalten

Ich möchte, dies zu tun Code verwenden und nicht durch xml.

Inzwischen funktioniert der folgende Code, aber das einzige, was es tut, ist, das Fragment durch SupportFragmentManager zu setzen, aber ich weiß nicht, wie ich seine Position mit dem ConstraintLayout ändern kann, weil ich es theoretisch verwenden könnte Fragment nach unten (derzeit geht es nach oben).

Meine Aktivität onCreate ist die folgende (es funktioniert, aber nicht so, wie ich will).

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    //TODO use constraintLayout to manage fragment position (ask) 
    ConstraintLayout activityLayout = (ConstraintLayout) findViewById(R.id.main_app_container); 
    if (activityLayout != null){ 
     if (savedInstanceState != null) { 
      return; 
     } 

     TabFragment tabFragment = TabFragment.newInstance(); 
     tabFragment.setArguments(getIntent().getExtras()); 

     getSupportFragmentManager() 
       .beginTransaction() 
       .add(R.id.main_app_container, tabFragment) 
       .commit(); 
    } 
} 

Mein ConstraintLayout xml (main_activity.xml) wird nach dem bei Bedarf:

<?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/main_app_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.xdxdxdxd.android.xdxdxdxd.MainActivity" 
    tools:layout_editor_absoluteY="0dp" 
    tools:layout_editor_absoluteX="0dp"> 
</android.support.constraint.ConstraintLayout> 

Sie Unten Bild sehen:

enter image description here

Antwort

0

Die Art und Weise frei zu wählen:

ConstraintSet constraintSet = new ConstraintSet(); 

    set.clone(activityLayout); 

    set.connect(ViewObject1, StartSide, ViewObject2, EndSide, Margin) 

    set.applyTo(activityLayout); 

Es gibt eine Reihe globaler Konstanten für die Anfangs-, End- und Objektwerte. Sie sollten die documents here auschecken.

Verwandte Themen