0

Je suis en train de poisition quelques textviews ci-dessous une autre vue de texte dans un RelativeLayout, mais pour une raison quelconque il ne fonctionne pas:Textviews dans RelativeLayout ne figurant pas en dessous de l'autre programatically

enter image description here

import android.graphics.Color; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.widget.TextViewCompat; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.GridLayout.LayoutParams; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 

import com.companyname.projectname.R; 

import static com.companyname.projectname.R.id.FL_relativeLayout; 


public class FragmentFL extends android.support.v4.app.Fragment { 

    public FragmentFL() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 

     return inflater.inflate(R.layout.fragment_fl, container, false); 
    } 

    @Override 
    public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
     View v = getView(); 
     assert v != null; 

     RelativeLayout relativelayout = v.findViewById(FL_relativeLayout); 

     RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

     // add text views 
     TextView txt1 = new TextView(getActivity()); 
     txt1.setText("Blue"); 
     TextViewCompat.setTextAppearance(txt1, android.R.style.TextAppearance_Large); 
     txt1.setTextColor(Color.BLACK); 

     TextView txt2 = new TextView(getActivity()); 
     txt2.setText("Black"); 
     TextViewCompat.setTextAppearance(txt2, android.R.style.TextAppearance_Medium); 
     txt2.setTextColor(Color.BLACK); 

     TextView txt3 = new TextView(getActivity()); 
     txt3.setText("Green"); 
     TextViewCompat.setTextAppearance(txt3, android.R.style.TextAppearance_Large); 
     txt3.setTextColor(Color.BLACK); 

     TextView txt4 = new TextView(getActivity()); 
     txt4.setText("Red"); 
     TextViewCompat.setTextAppearance(txt4, android.R.style.TextAppearance_Medium); 
     txt4.setTextColor(Color.BLACK); 

     TextView txt5 = new TextView(getActivity()); 
     txt5.setText("Yellow"); 
     TextViewCompat.setTextAppearance(txt5, android.R.style.TextAppearance_Large); 
     txt5.setTextColor(Color.BLACK); 

     TextView txt6 = new TextView(getActivity()); 
     txt6.setText("White"); 
     TextViewCompat.setTextAppearance(txt6, android.R.style.TextAppearance_Medium); 
     txt6.setTextColor(Color.BLACK); 

     txt1.setId(View.generateViewId()); 
     txt2.setId(View.generateViewId()); 
     txt3.setId(View.generateViewId()); 
     txt4.setId(View.generateViewId()); 
     txt5.setId(View.generateViewId()); 
     txt6.setId(View.generateViewId()); 

     rlp.addRule(RelativeLayout.BELOW, txt1.getId()); 
     rlp.addRule(RelativeLayout.BELOW, txt2.getId()); 
     rlp.addRule(RelativeLayout.BELOW, txt3.getId()); 
     rlp.addRule(RelativeLayout.BELOW, txt4.getId()); 
     rlp.addRule(RelativeLayout.BELOW, txt5.getId()); 
     txt2.setLayoutParams(rlp); 
     txt3.setLayoutParams(rlp); 
     txt4.setLayoutParams(rlp); 
     txt5.setLayoutParams(rlp); 
     txt6.setLayoutParams(rlp); 

     relativelayout.addView(txt1); 
     relativelayout.addView(txt2); 
     relativelayout.addView(txt3); 
     relativelayout.addView(txt4); 
     relativelayout.addView(txt5); 
     relativelayout.addView(txt6); 


     super.onActivityCreated(savedInstanceState); 
    } 
} 
+0

où vous spécifiez l'alignement? Si vous voulez que l'affichage du texte se trouve sous l'autre, pourquoi ne pas utiliser LinearLayour avec l'orientation verticale? – Swati

Répondre

2

Vous avez besoin d'utiliser différents LayoutParams pour chaque TextView comme dans

rlp.addRule(RelativeLayout.BELOW, txt1.getId()); 
rlp1.addRule(RelativeLayout.BELOW, txt2.getId()); 
rlp2.addRule(RelativeLayout.BELOW, txt3.getId()); 
rlp3.addRule(RelativeLayout.BELOW, txt4.getId()); 
rlp4.addRule(RelativeLayout.BELOW, txt5.getId()); 

    txt2.setLayoutParams(rlp); 
    txt3.setLayoutParams(rlp1); 
    txt4.setLayoutParams(rlp2); 
    txt5.setLayoutParams(rlp3); 
    txt6.setLayoutParams(rlp4);