2017-05-29 7 views
2

In einer Android-Anwendung versuche ich programmgesteuert benutzerdefinierte ConstraintLayout Ansichten zu einem vertikal orientierten LinearLayout hinzuzufügen.Festlegen der ConstraintLayout-Breite, um die übergeordnete Breite programmgesteuert anzupassen

Ich setze die LayoutParams auf MATCH_PARENT für die Breite und für die Höhe in der ConstraintLayout s. Jedoch, wenn ich die Anwendung ausführen, ist die ConstraintView alles zusammengeknüllt und der Inhalt überlappt sich. Im Folgenden finden Sie einige relevante Snippets und einen Screenshot meiner Anwendung. Wie gehe ich vor, um dieses Problem zu beheben?

public class ItemView extends ConstraintLayout { 
    LinearLayout linearButtons; 
    LinearLayout linearText; 
public ItemView(Context context, String name, String price, ArrayList<String> guests, 
    ArrayList<String> checked, int id) { 
... 
    addView(linearText); 
    addView(linearButtons); 
    set.clone(this); 
    set.connect(linearText.getId(), ConstraintSet.LEFT, this.getId(), 
     ConstraintSet.LEFT, 8); 
    set.connect(linearText.getId(), ConstraintSet.TOP, this.getId(), 
    ConstraintSet.TOP, 8); 
    set.connect(linearButtons.getId(), ConstraintSet.RIGHT, this.getId(), 
    ConstraintSet.RIGHT, 8); 
    set.connect(linearButtons.getId(), ConstraintSet.TOP, this.getId(), 
    ConstraintSet.TOP, 8); 
} 

anderswo:

for (Item it:r.getItems()) { 
     ItemView itemView = new ItemView(this, it.getName(), nf.format(it.getPrice()), dinerlist, it.getGuests(), i); 
     ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.MATCH_PARENT); 
     itemView.setLayoutParams(params); 
     vg.addV[enter image description here][1]iew(itemView); 
     Log.d("ItemView Children: ", itemView.getWidth()+" "+itemView.getHeight()); 

Antwort

2

In ConstraintLayout in xml, wenn Sie einen "MATCH_PARENT" width wollen, müssen Sie die Breite auf 0DP setzen und dann die layout_constraintWidth_default Attribut "Spread"

Programmatically können Sie das gleiche tun:

a) eine 0 dp Breite und Höhe

b) gesetzt defaultWidth und defaultHeight Einschränkungen

//add the view with 0dp width and height 
    val layoutParams = ConstraintLayout.LayoutParams(0, 0) 
    val view = View(context) 
    view.layoutParams = layoutParams 
    view.id = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) View.generateViewId() else 1138 
    parent.addView(view) 

    //apply the default width and height constraints in code 
    val constraints = ConstraintSet() 
    constraints.clone(parent) 
    constraints.constrainDefaultHeight(view.id, ConstraintSet.MATCH_CONSTRAINT_SPREAD) 
    constraints.constrainDefaultWidth(view.id, ConstraintSet.MATCH_CONSTRAINT_SPREAD) 
    constraints.applyTo(parent) 

Wenn Sie Java benötigen:

//add the view with 0dp width and height 
    ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(0, 0); 
    View view = View(context); 
    view.setLayoutParams(layoutParams); 
    view.setId(1138); 
    parent.addView(view); 

    //apply the default width and height constraints in code 
    ConstraintSet constraints = new ConstraintSet(); 
    constraints.clone(parent); 
    constraints.constrainDefaultHeight(view.getId(), ConstraintSet.MATCH_CONSTRAINT_SPREAD); 
    constraints.constrainDefaultWidth(view.getId(), ConstraintSet.MATCH_CONSTRAINT_SPREAD); 
    constraints.applyTo(parent); 
+0

leid in Kotlin zu beantworten, haben Sie Java benötigen? -> Ich habe Java hinzugefügt – Kaskasi

+0

Danke Kumpel Sie haben meinen Tag gerettet – Jinu

Verwandte Themen