Java 尝试添加多个视图,但得到一个空白屏幕
我创建了一个带有LinearLayout和TextView的自定义xml文件。我有一个for循环,我想通过一个ArrayList,为每个对象将视图添加到一个ScrollView。下面是我试图实现的代码,但它只是给了我一个空白屏幕。我试图查看ArrayList是否为空,但事实并非如此。当我尝试这个的时候,我真的不知道我在做什么。我只是想看看它是否有效,但没有那么有什么问题Java 尝试添加多个视图,但得到一个空白屏幕,java,android,Java,Android,我创建了一个带有LinearLayout和TextView的自定义xml文件。我有一个for循环,我想通过一个ArrayList,为每个对象将视图添加到一个ScrollView。下面是我试图实现的代码,但它只是给了我一个空白屏幕。我试图查看ArrayList是否为空,但事实并非如此。当我尝试这个的时候,我真的不知道我在做什么。我只是想看看它是否有效,但没有那么有什么问题 for(int x = 0; x < runs.size(); x++){ infla
for(int x = 0; x < runs.size(); x++){
inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.run_layout, (ViewGroup) getCurrentFocus());
TextView name = (TextView) layout.findViewById(R.id.tvRunName);
name.setText(runs.get(x).getName());
llRuns.addView(layout);
}
for(int x=0;x
下面是run_布局xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/run_background" >
<TextView
android:id="@+id/tvRunName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_gravity="center"
android:layout_marginTop="10dp" />
下面是活动xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".YourRuns"
android:background="#181818"
android:id="@+id/llYourRunsLayout">
<ScrollView
android:id="@+id/svRuns"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/llRuns"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
将(视图组)getCurrentFocus()更改为null,因为充气器。充气()第二个参数是视图的父参数,并且在启动默认活动时,没有带焦点的元素。您首先在充气时为视图指定两个父视图,然后在向llRuns添加布局时指定第二个父视图
for(int x = 0; x < runs.size(); x++){
inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.run_layout, (ViewGroup) getCurrentFocus());
TextView name = (TextView) layout.findViewById(R.id.tvRunName);
name.setText(runs.get(x).getName());
llRuns.addView(layout);
}
for(int x=0;x
也发布您的xml。此外,您是否考虑使用<代码> ListVIEW < /代码>?当有一个类似视图的列表时,它们有各种各样的优点。我试图添加到ScrollView或Activities xml的xml?您有没有理由不使用?至于我,布局看起来你不需要任何不同;并且使用列表视图
可以创建更具响应性的UI。为什么要使用(视图组)getCurrentFocus()
作为充气机的根节点;不确定它是否可以工作,但我假设您希望在那里使用null
?这不是完整的代码。您可能忘记调用setContentView()
或其他一些东西。您确定运行的中的元素包含非空文本吗?