Ich mache diese Titelseite meiner App. Leider wird nur die TextView
nicht angezeigt. Der Knopf und der Hintergrund laufen gut. Ich habe es sogar dem relativen Layout hinzugefügt, aber benutze die addView()
Funktion. Kann mir bitte jemand helfen?Meine App läuft gut, aber die TextView wird nicht angezeigt
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//layout
RelativeLayout mainLayout = new RelativeLayout(this);
mainLayout.setBackgroundColor(Color.BLUE);
//button
Button mainButton = new Button (this);
mainButton.setText("Next");
mainButton.setBackgroundColor(Color.RED);
//textview
TextView mainTextView = new TextView(this);
mainTextView.setText("Juliet Daniel Lab Molecular Biology Cancer Research Center");
mainTextView.setTextSize(30);
mainTextView.setBackgroundColor(Color.GREEN);
//sizing the button
RelativeLayout.LayoutParams btnDetails = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT
);
//sizing the textview
RelativeLayout.LayoutParams textViewDetails = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT
);
//give id's to position relatively...note it is preferable to do this in the res folder to avoid errors
mainButton.setId(1);
mainTextView.setId(2);
//give rules to position widgets relatively
textViewDetails.addRule(RelativeLayout.ABOVE, mainButton.getId());
textViewDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);
textViewDetails.setMargins(0,0,0,50);
//adding the button as a part of the RelativeLayout (ADDING THE BUTTON WIDGET TO LAYOUT)
mainLayout.addView(mainButton, btnDetails);
//adding the textview as a part of the relative Layout
mainLayout.addView(mainTextView, textViewDetails);
//setting the view as out layour
setContentView(mainLayout);
}
}
Antwort Munirs Versuchen. Nur ein Vorschlag, anstatt die Ids als 1,2 setzen Sie View.generateviewid() -Methode SetId – Raghavendra
Sie richten Ihre 'mainTextView' * über * Ihr' mainButton' - was das Hauptproblem ist. Sie sollten Layout-Parameter-Regeln von 'mainButton' in Bezug auf' mainTextView' verwenden; nicht umgekehrt. –