0

Ich versuche, in einem RelativeLayout einige Textviews unterhalb eines anderen Textansicht poisition, aber aus irgendeinem Grund funktioniert es nicht:Textviews in RelativeLayout nicht untereinander erscheinen programmatisch

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

wo legen Sie die Ausrichtung? Wenn Sie möchten, dass die Textansicht untereinander angezeigt wird, warum nicht LinearLayour mit vertikaler Ausrichtung verwenden? – Swati

Antwort

2

Sie benötigen verwenden, um verschiedene LayoutParams für jeden TextView wie in

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); 
Verwandte Themen