Android-dynamiczne dodawanie widoków do widoku
Mam układ widoku -
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="0px"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/items_header"
style="@style/Home.ListHeader" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/items_none"
android:visibility="gone"
style="@style/TextBlock"
android:paddingLeft="6px" />
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/items_list" />
</LinearLayout>
To, co chcę zrobić, jest w mojej głównej działalności z takim układem
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="0px"
android:id="@+id/item_wrapper">
</LinearLayout>
Chcę zapętlić mój model danych i wprowadzić wiele widoków składających się z pierwszego układu do głównego układu. Wiem, że mogę to zrobić, budując kontrolki całkowicie w kodzie, ale zastanawiałem się, czy istnieje sposób na dynamiczne budowanie widoków, aby móc nadal używać układu zamiast umieszczać wszystko w kodzie.
5 answers
Użyj LayoutInflater
aby utworzyć widok na podstawie szablonu układu, a następnie wprowadzić go do widoku tam, gdzie jest potrzebny.
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.your_layout, null);
// fill in any details dynamically here
TextView textView = (TextView) v.findViewById(R.id.a_text_view);
textView.setText("your text");
// insert into main view
ViewGroup insertPoint = (ViewGroup) findViewById(R.id.insert_point);
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
Być może będziesz musiał dostosować indeks, w którym chcesz wstawić widok.
Dodatkowo Ustaw LayoutParams zgodnie z tym, jak chcesz, aby pasowały do widoku nadrzędnego. np. z FILL_PARENT
, lub MATCH_PARENT
, itp.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-22 01:26:12
Zobacz LayoutInflater
klasy.
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup parent = (ViewGroup)findViewById(R.id.where_you_want_to_insert);
inflater.inflate(R.layout.the_child_view, parent);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-02 15:40:43
Wygląda to tak, jak naprawdę chcesz ListView z niestandardowym adapterem do napompowania określonego układu. Używając ArrayAdapter i metody notifyDataSetChanged()
masz pełną kontrolę nad generowaniem i renderowaniem widoków.
Spójrz na te tutoriale
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-02 15:53:37
Aby odpowiedź @Mark Fisher była bardziej jasna, wstawiony widok powinien być plikiem xml w folderze layout, ale bez układu (ViewGroup), takiego jak LinearLayout itp. do środka. Mój przykład:
Res / layout / my_view.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/i_am_id"
android:text="my name"
android:textSize="17sp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
Następnie punkt wstawiania powinien być układem podobnym do LinearLayout:
Res / layout / activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/aaa"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/insert_point"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</RelativeLayout>
Wtedy kod powinien być
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shopping_cart);
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.my_view, null);
ViewGroup main = (ViewGroup) findViewById(R.id.insert_point);
main.addView(view, 0);
}
Powodem, dla którego zamieszczam tę bardzo podobną odpowiedź jest to, że gdy próbowałem zaimplementuj rozwiązanie Marka, utknąłem na jakim pliku xml powinienem użyć do insert_point i widoku potomnego. Najpierw użyłem układu w widoku dziecka i całkowicie nie działał, co zajęło mi kilka godzin, aby dowiedzieć się. Więc mam nadzieję, że moje poszukiwania mogą zaoszczędzić czas innych.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-08 02:00:06
// Parent layout
LinearLayout parentLayout = (LinearLayout)findViewById(R.id.layout);
// Layout inflater
LayoutInflater layoutInflater = getLayoutInflater();
View view;
for (int i = 1; i < 101; i++){
// Add the text layout to the parent layout
view = layoutInflater.inflate(R.layout.text_layout, parentLayout, false);
// In order to get the view we have to use the new view with text_layout in it
TextView textView = (TextView)view.findViewById(R.id.text);
textView.setText("Row " + i);
// Add the text view to the parent layout
parentLayout.addView(textView);
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-26 11:28:51