Ich habe ein Beispielprojekt erstellt und "Hello Android Application" in Eclipse ausgeführt.
Ich habe gelernt, dass eine Textansicht auf zwei Arten erstellt werden kann, entweder mit einem XML-Tag oder mit Java-Code.
Standardmäßig habe ich in meinem Beispielprojekt eine Textansicht mit der Aufschrift "Hallo Welt". Ich möchte eine Textansicht mit Java-Code erstellen und darauf eine Nachricht anzeigen.
Ich habe viel gesucht, kann aber die im Code genannten Schritte und Layouteinstellungen nicht verstehen.
Das habe ich getan:
import Android.app.Activity;
import Android.view.Menu;
import Android.view.ViewGroup;
import Android.widget.*;
import Android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 0.0F);
TextView tx= new TextView(this);
// tx.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tx.setText("Android APP");
lay
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Außerdem weiß ich nicht, wie ich diese Textansicht in addView()
hinzufügen kann.
Dies ist mein activity_main.xml
:
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
tools:context=".MainActivity" >
</RelativeLayout>
Eine Schritt-für-Schritt-Lösung wäre für mich hilfreich und jeder gute Tutorial-Link wäre spürbar. Danke im Voraus!
Verwenden Sie diesen Code, erstellen Sie eine Textansicht und legen Sie Layout-Parameter fest
TextView dynamicTextView = new TextView(this);
dynamicTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
dynamicTextView.setText(" Hello World ");
fügen Sie diese Textansicht zum Hauptlayout hinzu
mainlayout.addView(dynamicTextView);
.xml Datei
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:paddingBottom="@dimen/activity_vertical_margin"
Android:paddingLeft="@dimen/activity_horizontal_margin"
Android:paddingRight="@dimen/activity_horizontal_margin"
Android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.demo.MainActivity" >
<TextView
Android:id="@+id/textView1"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"/>
</LinearLayout>
.Java-Datei
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String name="Prakash Gajera";
TextView tv=(TextView)findViewById(R.id.textView1);
tv.setText(name);
}
kopieren Sie diesen Code und fügen Sie ihn ein. Ich hoffe, er wird Ihnen helfen.
import Android.app.Activity;
import Android.view.Menu;
import Android.view.ViewGroup;
import Android.widget.*;
import Android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout ll = new LinearLayout(this);
TextView tx= new TextView(this);
tx.setText("Android APP");
ll.addView(tx);
setContentView(ll);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
fügen Sie die Textansicht wie folgt zum linearen Layout hinzu . linearLayout.addView (textView).
Bevor Sie eine Instanz für das lineare Layout erstellen.
Es wird empfohlen, XML zur Definition von Layouts zu verwenden. Erstellen Sie View
s nur, wenn Sie sie dynamisch erstellen müssen.
Wenn Sie wirklich TextView
s anhand des Codes erstellen möchten, müssen Sie einen Verweis auf das übergeordnete Layout haben. Anstatt die Inhaltsansicht direkt auf ein XML-Layout festzulegen, müssten Sie das XML-Layout aufblasen und dann die Inhaltsansicht auf View
setzen. Beispiel:
View view = LayoutInflater.from(this).inflate(R.layout.activity_main, null);
setContentView(view);
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 0.0F);
TextView tx= new TextView(this);
// tx.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tx.setText("Android APP");
view.addView(tx); //here the textview is attached to the parent
Code:
TextView textView = new TextView(this);
textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
textView.setText("Test");
mainlayout.addView(textView );
versuche dies
import Android.app.Activity;
import Android.view.Menu;
import Android.view.ViewGroup;
import Android.widget.*;
import Android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout = (LinearLayout)findViewById(yourlayoutid from xml file);
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 0.0F);
TextView tx= new TextView(this);
// tx.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tx.setText("Android APP");
layout.add(tx);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Angenommen, Sie haben ein Stammlayout in der XML-Datei mit der ID "my_root".
LinearLayout my_root = (LinearLayout) findViewById(R.id.my_root);
Erstellen Sie ein neues Layout:
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
Erstellen Sie eine TextView:
TextView textView = new TextView(this);
Legen Sie einen Text fest:
textView.setText("some text");
Fügen Sie Ihre Textansicht zum Layout hinzu:
layout.addView(textView);
Fügen Sie schließlich Ihr Layout zum Stammlayout hinzu:
my_root.addView(layout);
Design
<TextView
Android:id="@+id/textview"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"/>
Java
TextView tv = findViewById(R.id.textview);
tv.setText("Kirubha");