5
votes

Android - ConstraintLayout a défini le pourcentage de hauteur par programmation?

voici ma mise en page:

ConstraintSet set = new ConstraintSet();
set.constrainPercentHeight(R.id.myclayout, (float) 0.4);
set.applyTo(((ConstraintLayout) vw.findViewById(R.id.myclayout)));

comment définir constraintHeight_percent par programmation?

J'ai essayé avec ConstraintSet mais cela n'a pas fonctionné

...
<android.support.constraint.ConstraintLayout   
android:layout_width="0dp"
android:layout_height="match_parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.6"
>
            <android.support.constraint.ConstraintLayout
                android:id="@+id/myclayout"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                app:layout_constraintHeight_percent="0.2"
                app:layout_constraintTop_toTopOf="parent"
                ></android.support.constraint.ConstraintLayout>


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


2 commentaires

Vérifiez ma réponse stackoverflow.com/a/54230893/10271334 , vous pouvez en obtenir une référence.


Pour ce que ça vaut, un avantage majeur du ConstraintLayout est d'éviter l'imbrication; les voir imbriquées les unes dans les autres est un drapeau rouge. Merci.


3 Réponses :


11
votes

la bonne réponse est:

    ConstraintLayout mConstrainLayout  = (ConstraintLayout) vw.findViewById(R.id.myclayout);
    ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) mConstrainLayout.getLayoutParams();
    lp.matchConstraintPercentHeight = (float) 0.4;
    mConstrainLayout.setLayoutParams(lp);


0 commentaires

0
votes

J'ai utilisé un ConstraintSet pour mettre à jour la largeur et la hauteur d'un enfant ConstraintLayout :

        val set = ConstraintSet()
        set.clone(parentLayout) // parentLayout is a ConstraintLayout
        set.constrainPercentWidth(childElement.id, .5f)
        set.constrainPercentHeight(childElement.id, .5f)
        set.applyTo(parentLayout)

... le float les valeurs doivent être un pourcentage mappé à l'intervalle unitaire.


0 commentaires

0
votes

J'ai eu la même question et j'ai heureusement trouvé ce message. J'ai développé une application d'instruments de musique virtuels, une application de batterie / casserole en acier avec huit interfaces utilisateur différentes ayant des éléments principaux de forme circulaire. J'ai utilisé une mise en page pour toutes les tailles d'écran pour chacune des huit interfaces utilisateur afin de réduire la redondance de la mise en page et je voulais conserver le rapport hauteur / largeur des éléments circulaires sur la plupart des appareils avec des résolutions d'écran différentes. J'ai donc dû utiliser layout_constraintWidth_percent et layout_constraintHeight_percent pour redimensionner la mise en page parent en fonction du rapport hauteur / largeur de l'écran de l'appareil. J'ai utilisé le code de @ débutant ci-dessus et combiné avec du code pour calculer le rapport hauteur / largeur de l'écran et la mise en œuvre conséquente de l'idée. Voici le code:

// instantiate constraint layout inside OnCreate
ConstraintLayout mcLayout =findViewById(R.id.layout_myConstraintLayout);
    
//determine display aspect ratio
WindowManager wm = (WindowManager) 
this.getSystemService(Context.WINDOW_SERVICE);
assert wm != null;
Display display = wm.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
assert display != null;
display.getMetrics(metrics);
float width = metrics.widthPixels;
float height = metrics.heightPixels;
float ratio = width/height;
float aspectRatio = round(ratio, 2);
Log.d("DISPLAYRATIO", "Display Ration is: " + displayRatio);

// create contraintlayout set for mConstraintLayout and params
ConstraintSet set = new ConstraintSet();
ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) 
lowTenorBkg.getLayoutParams();

// set percent width and height acacording to screen display aspect ratio
if (aspectRatio > 1.30f && aspectRatio < 1.42f) {
    // Add constraints
    lp.matchConstraintPercentWidth = 0.96f;
    lp.matchConstraintPercentHeight = 0.78f;
} else if (aspectRatio > 1.40f && aspectRatio < 1.60f) {
    // Add constraints
    lp.matchConstraintPercentWidth = 0.96f;
    lp.matchConstraintPercentHeight = 0.83f;
} else if (aspectRatio > 1.58f && aspectRatio < 1.67f) {
    // Add constraints
    lp.matchConstraintPercentWidth = 0.96f;
    lp.matchConstraintPercentHeight = 0.93f;
} else if (aspectRatio > 1.65f && aspectRatio <= 1.71f) {
    // Add constraints
    lp.matchConstraintPercentWidth = 0.92f;
    lp.matchConstraintPercentHeight = 0.94f;
} else if (aspectRatio > 1.72f && aspectRatio <= 1.78f) {
    // Add constraints
    lp.matchConstraintPercentWidth = 0.875f;
    lp.matchConstraintPercentHeight = 0.94f;
} else if (aspectRatio > 1.78f && aspectRatio <= 2.05f) {
    // Add constraints
    lp.matchConstraintPercentWidth = 0.8f;
    lp.matchConstraintPercentHeight = 0.93f;
}

lowTenorBkg.setLayoutParams(lp);
set.applyTo(mcLayout);

// put the round method inside the activity class 
// method for rounding float number to decimal places
public static float round(float d, int decimalPlace) {
   return BigDecimal.valueOf(d).setScale(decimalPlace,
   BigDecimal.ROUND_HALF_UP).floatValue();
}


0 commentaires