Próbuję uzyskać przycisk dodawania, aby dodać inny przycisk do układu, w oparciu o edittext po lewej stronie przycisku. Chodzi o to, aby osoba wymieniała pokoje w ich domach, a kiedy wpisują w każdym pokoju, generowany jest nowy przycisk, aby mogli kliknąć pokój, a następnie rozpocząć pracę na następnej stronie.Android: programowo dodaje przyciski do układu
Miałem układ XML wszystko gotowe, a następnie zorientowałem się, że jestem "programowo" dodając przyciski, więc programowo zmieniam układ, a następnie w przełączniku/case (tak robię onclicks) dla przycisku dodawania Próbowałem dodać przycisk do widoku, ale robi się to bardzo trudne. Chciałbym mieć przewijany widok pod edittextem i dodawać przyciski, a kiedy dodają wszystkie pokoje do swojego domu, w końcu jest wypełniona przewijaną listą przycisków dla całego ich domu. Czy istnieje sposób na programowe dodawanie przycisków do układu xml'd. Myślałem, że możesz, ale wszystko, co próbuję, nie działa.
Dzięki za pomoc dla wszystkich, wszelkie rekomendacje, które masz, byłyby bardzo docenione.
Pierwsza Edycja (w odpowiedzi na rozwiązanie Tanuj'S)
moim pliku XML (nie wiem, czy będziemy używać to lub po prostu użyć Java):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvAddARoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tvAddARoom" />
<EditText
android:id="@+id/etAddARoom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/etAddARoom" />
<Button
android:id="@+id/btnAddARoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnAdd" />
<TextView
android:id="@+id/tvSelectARoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tvSelectARoom" />
<TextView
android:id="@+id/tvNoRooms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tvNoRooms" />
<Button
android:id="@+id/btnViewAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnViewAll" />
</LinearLayout>
a Java. To nie jest w ogóle poprawne, ponieważ w java tworzę cały układ zamiast używać powyższego układu. Tylko nie jestem pewien, czy mogę połączyć te dwa.
package com.bluej.movingbuddy;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
//import android.widget.ScrollView;
import android.widget.TextView;
public class EstimatorByRoom extends Activity implements OnClickListener {
String roomName;
EditText etAddARoom;
LinearLayout layout;
LinearLayout.LayoutParams layoutParam;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//setContentView(R.layout.estimatorbyroom);
LayoutParams params =
new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
//create a layout
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
//create a text view
TextView tvAddARoom = new TextView(this);
tvAddARoom.setText("Add a Room");
tvAddARoom.setLayoutParams(params);
//create an edittext
EditText etAddARoom = new EditText(this);
etAddARoom.setHint("Living Room, Dining Room, etc.");
etAddARoom.setLayoutParams(params);
//create a button
Button btnAddARoom = new Button(this);
btnAddARoom.setText("Add");
btnAddARoom.setLayoutParams(params);
//adds the textview
layout.addView(tvAddARoom);
//add the edittext
layout.addView(etAddARoom);
//add the button
layout.addView(btnAddARoom);
//create the layout param for the layout
LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
this.addContentView(layout, layoutParam);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnAddARoom:
//add a room
//this part isn't working!
roomName = etAddARoom.getText().toString();
Button createdButton = new Button(this);
createdButton.setText(roomName);
layout.addView(createdButton);
this.addContentView(layout, layoutParam);
//if no rooms make tvnorooms disappear
break;
}
}
}
spróbuj usunąć 'this.addContentView (układ, layoutParam);' –
to może pomóc http: // stackoverflow.com/questions/4269660/dynamically-adding-content-to-relativelayout –