Я пытаюсь добавить динамическое содержимое в представление, созданное с помощью XML.
У меня есть представление "profile_list", в котором есть ScrollView, к которому я хотел бы добавить некоторые элементы.
Вот код того, что я пытаюсь сделать.
// Find the ScrollView
ScrollView sv = (ScrollView) this.findViewById(R.id.scrollView1);
// Create a LinearLayout element
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
// Add text
tv = new TextView(this);
tv.setText("my text");
ll.addView(tv);
// Add the LinearLayout element to the ScrollView
sv.addView(ll);
// Display the view
setContentView(R.layout.profile_list);
План состоит в том, чтобы добавить TableLayout и заполнить его динамически, а не просто фиктивным текстом, но сначала я должен заставить это работать.
Любая помощь приветствуется.
С уважением Олле
Я нашел решение!
Глупо, я оставил элемент в своем ScrollView в своем XML-файле!
В любом случае, это рабочий пример:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.profile_list, null);
// Find the ScrollView
ScrollView sv = (ScrollView) v.findViewById(R.id.scrollView1);
// Create a LinearLayout element
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
// Add text
TextView tv = new TextView(this);
tv.setText("my text");
ll.addView(tv);
// Add the LinearLayout element to the ScrollView
sv.addView(ll);
// Display the view
setContentView(v);
И XML-файл дляmatch
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView
android:id="@+id/scrollView1"
android:clickable="true"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_marginBottom="50px"
android:layout_height="fill_parent">
</ScrollView>
</LinearLayout>
Надеюсь, это кому-нибудь поможет.Спасибо за помощь!