Android Viele Buttons und ein Layout

Der

Mitglied
Hallo,
Wenn ich in Java mal 100 Buttons erstellen möchte, dann kann ich das schnell über eine Schleife machen (eventuell davor noch ein neues Layout setzen).
Wenn ich aber für Android programmiere, dann muss ich entweder jeden Button einzeln in xml beschreiben oder in dem Graphischen Layout jeden Button einzeln reinziehen, wenn ich dann 100 Buttons haben möchte, dann hab ich eine weile zu tun....
Irgendwie muss man also doch auch 100 Buttons mit einer for-Schleife und einem Layout erstellen können. Wie geht das?

Danke im Vorraus!
 

Der

Mitglied
also:
// view ist einer instanz von der Klasse View? also wie deklariere und initialisiere ich view dann?
Button einButton = new Button (...);
view.add(einButton, ...);
setContentView(view);
 

dzim

Top Contributor
Du weisst schon, wie man eine Activity oder ein Fragment mit einem Layout befüllt, oder? Wenn nein, dann schau dir das erst an, denn es erklärt sich eigentlich von selbst...

Grundsätzlich aber wäre dein Ansatz irgendwie so: Baue dir ein Layout aus einem ScrollView (fülle die ganze Breite und Höhe des Screens), dass ein LinearLayout enthält (Achtung: setze dir eine ID, damit du es im Code referenzieren kannst!). Und im Code fügst du dann die Buttons hinzu, wie der JavaMeister es beschrieben hat (der View wäre dann über die bereits genannte ID zu holen - und auf LinearLayout zu casten).
 

Der

Mitglied
ich versteh kein wort....

kannst mir nicht einfach ein beispiel geben? Also den programmcode, vielleicht kannst ihn mir noch auskommentieren, das wär super... Ober muss man das dann doch teils in xml beschreiben?
 

dzim

Top Contributor
Ernsthaft? Soll ich dir auch gleich noch die ganze App programmieren? Ich glaube, du solltest dich erst einmal mit den grundlegenden Tutorials beschäftigen, denn sonst gebe ich dir nur Code, den du nicht verstehst.

Natürlich kannst du grundsätzlich deine Oberfläche komplett im Code beschreiben, aber a) vermischt man dann zu sehr View und Control und b) ist es viel zu kompliziert!

Lies dir mal dieses Tutorial durch:
Android Development - Tutorial

Und wenn du schon dabei bist, auch gleich noch so viele der anderen Tutorials auf Lars Vogels Seite, wie möglich.

Btw: Ich deklariere fast alle Oberflächen in XML und füge sie per LayoutInflater an den stellen Hinzu, wenn ich im Code eine gewisse Dynamic brauche.

Das Layout wäre etwas wie das hier
[XML]
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
android:id="@+id/linearContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:eek:rientation="vertical" >

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="test" />
</LinearLayout>

</ScrollView>
[/XML]
Der Button ist ein Beispiel. Im Code, wo du das Layout z.B. per
Code:
setContentView(r.layout.test)
in einer Activity (oder per LayoutInflater in einem Fragment) erstellst, besorgst du dir dann anschliessend den Container für deine Buttons per
Code:
LinearLayout container = (LinearLayout)findViewById(R.id.linearContainer);
(im Fragment: auf dem gerade inflatete
Code:
View
musst du natürlich das
Code:
#findeViewById(int)
durchführen) und fügst da deine Buttons im Code hinzu.
Ungefähr so (ungetested und die Buttons sind natürlich unbeschriftet...):
Java:
setContentView(R.layout.test);
		LinearLayout container = (LinearLayout) findViewById(R.id.linearContainer);
		for (int i = 0; i < 100; i++) {
			Button b = new Button(this);
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
			container.addView(b, lp);
		}

So der Herr. Und ab jetzt liest du Tutorials, bis die Schwarte kracht, denn das sind grundlegende Sachen, die man eigentlich problemlos(!!!), wie so ziemlich jeder andere Android-Entwickler bestätigen wird, im Selbststudium lernen kann.
Wenn du Fragen zu Services hast, ist das etwas anderes, weil das weit komplexer ist, aber wie man eine solch simple UI bastelt, sollte man auch ohne Hilfe hinbekommen.
 

Der

Mitglied
Super! danke, dass du dir so viel mühe gegeben hast, ich werd mir ein Paar toturials andschauen!

Damit wär meine Frage dann wohl (selbst für leute die überhaupt keine Ahnung haben ;D) beantwortet.
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
S onClickListener auf Buttons die im Code erzeugt wurden setzen. Android & Cross-Platform Mobile Apps 8
L Hintergrund eines Switch(Buttons) ändern Android & Cross-Platform Mobile Apps 3
D Android dynamische Buttons positionieren Android & Cross-Platform Mobile Apps 1
N Android Zwei Buttons gleichzeitig drücken Android & Cross-Platform Mobile Apps 9
H Buttons färben sich mit, beim Hintergrund ändern Android & Cross-Platform Mobile Apps 3
D Hilfe! es geht um Buttons und Sounds Android & Cross-Platform Mobile Apps 14
K Dynamische (Image)Buttons, Größe bestimmen Android & Cross-Platform Mobile Apps 4
V Buttons bei Klick hinzufügen Android & Cross-Platform Mobile Apps 2
V Buttons zur Actionbar hinzufügen Android & Cross-Platform Mobile Apps 4
L Android Bildschirm bleibt dunkel nach neustarten der App nach betätigen des Home-Buttons Android & Cross-Platform Mobile Apps 3
K Verständisfrage bzgl. Buttons und ihnen eine Funktion geben. Android & Cross-Platform Mobile Apps 3
P Buttons im + anordnen Android & Cross-Platform Mobile Apps 2
T Android AlertDialog; Buttons ändern Android & Cross-Platform Mobile Apps 4
H Buttons werden falsch angeordent Android & Cross-Platform Mobile Apps 2
F Buttons und awt? Android & Cross-Platform Mobile Apps 7
W Ausklappbares Verlängertes XML Layout Android & Cross-Platform Mobile Apps 6
W Android Wieso kann ich keine ListView mehr zum Layout hinzufügen? Android & Cross-Platform Mobile Apps 1
A Android-Studio: 2. Layout nach kurzer Zeit aufzeigen Android & Cross-Platform Mobile Apps 2
S Android Layout - welchen Typ? Android & Cross-Platform Mobile Apps 3
B Android Multiple Screens- layout-sw600dp Android & Cross-Platform Mobile Apps 1
D Android Layout für alle Geräte Android & Cross-Platform Mobile Apps 4
W Android Designfrage / Layout / Activity / Fragments Android & Cross-Platform Mobile Apps 2
B Layout Bibliothek Android & Cross-Platform Mobile Apps 8
B Android Spiele-Layout umsetzen Android & Cross-Platform Mobile Apps 5
O zurück Schaltfläche in voriges Layout Android & Cross-Platform Mobile Apps 5
B Erste Android-App: setContentView(R.layout.main) funktioniert nicht Android & Cross-Platform Mobile Apps 6
M Android Game, welche Layout? Android & Cross-Platform Mobile Apps 2
B Eigene View xml-Layout einbinden Android & Cross-Platform Mobile Apps 1
R Android Layout Bild mit Text Android & Cross-Platform Mobile Apps 13
K Rand bei (Table,Relative,Linear)Layout - wie bekomme ich ihn weg? Android & Cross-Platform Mobile Apps 3
D Android Layout Problem Android & Cross-Platform Mobile Apps 2
P Android Nach Animation Layout auf alten Platz Android & Cross-Platform Mobile Apps 3
T Android Layout Update Animation Android & Cross-Platform Mobile Apps 3
P Probleme mit xml-Layout Android & Cross-Platform Mobile Apps 2
W XML Layout: wann wird geladen? Android & Cross-Platform Mobile Apps 10
F Layout mit listViews (Scrolling-Probleme) Android & Cross-Platform Mobile Apps 2
G Fehlermeldung: "No XML content. Please add a root view or layout to your documet." Android & Cross-Platform Mobile Apps 7
N Android xml - graphical layout nur noch weiß :o Android & Cross-Platform Mobile Apps 4
T Android Merkwürdigkeiten im Layout Android & Cross-Platform Mobile Apps 7
JAVAnnik Android Layout ändern in Thread Android & Cross-Platform Mobile Apps 2
A Android Browser öffnen, XML-GUI-Layout Android & Cross-Platform Mobile Apps 23
A Absolute Layout soll auf jedem Gerät gleich aussehen Android & Cross-Platform Mobile Apps 4
S Android Layout Problem mit fill_parent Android & Cross-Platform Mobile Apps 5
tfa Android Layout-Probleme: View programmatisch erweitern (addContentView) Android & Cross-Platform Mobile Apps 7

Ähnliche Java Themen

Neue Themen


Oben