2016-08-09 6 views
1

Ich versuche, eine Ansicht von Java dynamisch zu erstellen, und ich bin verwirrt, warum dies nicht funktioniert.TextView nicht in RelativeLayout angezeigt

Das TextView displayText wird überhaupt nicht auf dem Bildschirm angezeigt. Ich sehe nicht, warum es nicht wie oben erwähnt wäre, wo der Benutzer seinen Namen eingibt.

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     RelativeLayout ceriLayout = new RelativeLayout(this); 

     Button genButton = new Button(this); 
     genButton.setText("Press here"); 
     genButton.setId(0); 

     TextView displayView = new TextView(this); 
     displayView.setText("What is going to go here huh"); 
     displayView.setId(1); 

     EditText nameView = new EditText(this); 
     nameView.setText("Enter your name"); 
     nameView.setId(2); 

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

     RelativeLayout.LayoutParams name = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.WRAP_CONTENT, 
       RelativeLayout.LayoutParams.WRAP_CONTENT 
     ); 
     RelativeLayout.LayoutParams display; 
     display = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.WRAP_CONTENT, 
       RelativeLayout.LayoutParams.WRAP_CONTENT 
     ); 

     button.addRule(RelativeLayout.CENTER_HORIZONTAL); 
     button.addRule(RelativeLayout.CENTER_VERTICAL); 

     display.addRule(RelativeLayout.CENTER_HORIZONTAL); 
     display.addRule(RelativeLayout.ABOVE, nameView.getId()); 

     name.addRule(RelativeLayout.CENTER_HORIZONTAL); 
     name.addRule(RelativeLayout.ABOVE, genButton.getId()); 



     ceriLayout.addView(genButton, button); 
     ceriLayout.addView(nameView,name); 

     setContentView(ceriLayout); 

    } 
} 
+2

Wow, langsamer Mensch. Entwerfen Sie Ihr gesamtes Layout mit Code? Wussten Sie, dass Sie XML-Dateien als Layout verwenden können? –

+0

'ceriLayout.addView (displayView ...'? Wo ist es? –

Antwort

2

Sie fügen nicht displayView in Ihrem ceriLayout. Deshalb taucht es nicht auf.

Ein Rat: Bitte versuchen Sie Ihre View Komponenten innerhalb .xml Layout-Dateien zu lernen, zu schreiben und diese Layouts als Inhaltsansichten Aktivitäten eingestellt.

Verwandte Themen