Android 向静态布局添加视图
我是android程序员的第一天,我的问题是:在我的应用程序中,我有一个使用静态布局的支持活动;我在onCreate方法中的活动接收一个数据列表-我应该如何将这些数据嵌入到现有布局中 到目前为止,我已经尝试了这段代码,但它没有显示任何内容,尽管没有异常,元素也很少,因此循环工作: 活动类别:Android 向静态布局添加视图,android,dynamic,view,Android,Dynamic,View,我是android程序员的第一天,我的问题是:在我的应用程序中,我有一个使用静态布局的支持活动;我在onCreate方法中的活动接收一个数据列表-我应该如何将这些数据嵌入到现有布局中 到目前为止,我已经尝试了这段代码,但它没有显示任何内容,尽管没有异常,元素也很少,因此循环工作: 活动类别: try { super.onCreate(savedInstanceState); setContentView(R.layout.results);
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.results);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.results, null);
// Find the ScrollView
LinearLayout sv = (LinearLayout) v.findViewById(R.id.lMain);
// getting data here
for (int i = 0; i < recipesList.getId().size(); i++) {
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
// Add text
TextView tv = new TextView(this);
tv.setText("abc");
ll.addView(tv);
sv.addView(ll);
}
试试看{
super.onCreate(savedInstanceState);
setContentView(R.layout.results);
LayoutFlater充气器=(LayoutFlater)getSystemService(Context.LAYOUT\u充气器\u服务);
视图v=充气机充气(R.layout.results,null);
//找到滚动视图
线性布局sv=(线性布局)v.findViewById(R.id.lMain);
//在这里获取数据
对于(int i=0;i
results.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/lMain" >
</LinearLayout>
</ScrollView>
你的建议是错误的。你必须这样做
try
{
super.onCreate(savedInstanceState);
setContentView(R.layout.results);
LinearLayout sv = (LinearLayout) findViewById(R.id.lMain);
// getting data here
for (int i = 0; i < recipesList.getId().size(); i++)
{
// Add text
TextView tv = new TextView(this);
tv.setText("abc");
sv .addView(tv);
}
}
试试看
{
super.onCreate(savedInstanceState);
setContentView(R.layout.results);
LinearLayout sv=(LinearLayout)findViewById(R.id.lMain);
//在这里获取数据
对于(int i=0;i
什么是recipesList?它来自哪里?它是一个我需要显示的数据列表,正如我所说的循环工作,我只需要了解如何将文本等添加到我的静态视图中