无法使 ScrollView 和 textView(以编程方式添加)在 Android 中可见
unable to make the ScrollView and textView (added programmatically) visible in Android
我正在做一个 android 项目,我必须从 JSONArray 写入数据。我尝试对每个数组输入使用 TextView。所以我曾经编写代码在我现有的布局上添加一个 scrollView 并添加一个 TextView init。代码运行完美,没有任何错误,但是,当我编译它时 UI 中没有任何内容。 (在我的手机上)后来,我尝试将另一个 TextView 添加到现有的 LinearLayout 而不是 ScrollView,那次它也没有用。我检查了其他问题和论坛,在我看来,我已经 done/wrote 他们所做的一切,但我仍然找不到我的错误。
更新 1:我在另一个 LinearLayout 中添加了 ScrollView,并在其中保留了另一个 LinearLayout。 TextView 被添加到 ScrollView 内的 LinearLayout 内。外部 LinearLayout 持有另一个 TextView 只是 post 异常日志(我的 avd 内核崩溃)。
更新 2 我已经设置了 scrollView 的高度 WRAP_CONTENT 并在其下方添加了另外四个 TextView。现在,正在加载的应用程序显示 Top TextView 和最后一个 TextView 组之间存在相当大的差距。但是,ScrollView 仍然没有显示任何内容。
我的代码已更新 & 如下所示:
Java代码:
TextView t1 = (TextView)findViewById(R.id.t1);
LinearLayout mainLinear = (LinearLayout)findViewById(R.id.mainLinear);
try {
LinearLayout linear =(LinearLayout)findViewById(R.id.scroll_container);
linear.setBackgroundColor(0xABCDEF);
//for (int i = 0; i < 10; i++) {
TextView tv = new TextView(getApplicationContext());
tv.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
));
tv.setTextSize(25);
//tv.setId(1);
tv.setTextColor(0x000000);
tv.setPadding(10, 10, 10, 10);
tv.setText("I am serial " + 0 + "\n" + "is there something more");
linear.addView(tv);
TextView tv1 = new TextView(getApplicationContext());
tv1.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
));
tv1.setTextSize(25);
//tv.setId(1);
tv1.setTextColor(0x000000);
tv1.setPadding(10, 10, 10, 10);
tv1.setText("I am serial 3 " + 1 + "\n" + "is there something more");
linear.addView(tv1);
//tv.append("my name is apple " + 0 + "\n");
TextView tv5 = new TextView(getApplicationContext());
tv5.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
));
tv5.setTextSize(25);
//tv.setId(1);
tv5.setTextColor(0x000000);
tv5.setPadding(10, 10, 10, 10);
tv5.setText("Linear Layout e" + 0 + "\n");
mainLinear.addView(tv5);
// }
}
catch (Exception e)
{
e.printStackTrace();
t1.setText("<><>"+e);
}
我的布局XML文件:
<LinearLayout
android:id="@+id/mainLinear">
<TextView android:text="Word: Monkey Kaka"
android:textSize="25dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/t1"
android:textColor="#000000"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/ScrollView">
<LinearLayout
android:id="@+id/scroll_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:text="Test 1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test 2"
android:textSize="25dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test 3"
android:textSize="25dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test 4"
android:textSize="25dp"
/>
</LinearLayout>
感谢您的帮助。
ScrollView
小部件只能承载一个直接子项。
您的布局应如下所示:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/scroll_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
然后从你的代码中你可以添加 TextView
s 到 LinearLayout
:
LinearLayout scrollContainer = (LinearLayout) findViewById(R.id.scroll_container);
scrollContainer.addView(tv);
你应该看看LogCat
。它可能会告诉你:
java.lang.IllegalStateException: ScrollView can host only one direct child
来自 ScrollView:245
source code:
@Override
public void addView(View child) {
if (getChildCount() > 0) {
throw new IllegalStateException("ScrollView can host only one direct child");
}
super.addView(child);
}
编辑:(见第三条评论)
这就是当您执行 mainLinear.addView(tv1);
时会发生的情况。
您可以在 Android Studio 布局 Preview
.
中的 res/layout/scroll_test.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"
android:padding="10dp"
android:layout_margin="10dp"
android:orientation="vertical"
tools:context="me.ibtehaz.greapp.showWord"
android:id="@+id/mainLinear">
<TextView android:text="Word: Monkey Kaka"
android:textSize="25dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/t1"
android:textColor="#000000"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/ScrollView">
<LinearLayout
android:id="@+id/scroll_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test 1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test 2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test 3" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test 4" />
</LinearLayout>
现在,如果您将 ScrollView
布局高度设置为 wrap_content
,您将看到那 4 个 TextView
出现。
与其说是代码问题,不如说是UI设计问题。
我明白了。在所有情况下,我都为背景提供了错误的 hexValue。我将布局参数从 ViewGroup 更改为线性(我不知道为什么,在教程中找到它)。最后我使用了一个 TextView 数组,而不是重复创建一个对象。
感谢您的帮助:)
TextView t1 = (TextView)findViewById(R.id.t1);
LinearLayout mainLinear = (LinearLayout)findViewById(R.id.mainLinear);
try {
LinearLayout linear = (LinearLayout)findViewById(R.id.scroll_container);
linear.setBackgroundColor(0xABCDEFFE);
TextView []tv = new TextView[10];
for (int i = 0; i < 10; i++) {
tv[i] = new TextView(getApplicationContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
tv[i].setLayoutParams(params);
tv[i].setTextSize(25);
tv[i].setId(i);
tv[i].setTextColor(Color.BLACK);
tv[i].setPadding(10, 10, 10, 10);
tv[i].setBackgroundColor(Color.WHITE);
tv[i].setText("I am serial 4 " + i + "\n" + "is there something more");
linear.addView(tv[i]);
}
}
catch (Exception e)
{
e.printStackTrace();
t1.setText(value+"<><>"+e);
}
我正在做一个 android 项目,我必须从 JSONArray 写入数据。我尝试对每个数组输入使用 TextView。所以我曾经编写代码在我现有的布局上添加一个 scrollView 并添加一个 TextView init。代码运行完美,没有任何错误,但是,当我编译它时 UI 中没有任何内容。 (在我的手机上)后来,我尝试将另一个 TextView 添加到现有的 LinearLayout 而不是 ScrollView,那次它也没有用。我检查了其他问题和论坛,在我看来,我已经 done/wrote 他们所做的一切,但我仍然找不到我的错误。
更新 1:我在另一个 LinearLayout 中添加了 ScrollView,并在其中保留了另一个 LinearLayout。 TextView 被添加到 ScrollView 内的 LinearLayout 内。外部 LinearLayout 持有另一个 TextView 只是 post 异常日志(我的 avd 内核崩溃)。
更新 2 我已经设置了 scrollView 的高度 WRAP_CONTENT 并在其下方添加了另外四个 TextView。现在,正在加载的应用程序显示 Top TextView 和最后一个 TextView 组之间存在相当大的差距。但是,ScrollView 仍然没有显示任何内容。
我的代码已更新 & 如下所示:
Java代码:
TextView t1 = (TextView)findViewById(R.id.t1);
LinearLayout mainLinear = (LinearLayout)findViewById(R.id.mainLinear);
try {
LinearLayout linear =(LinearLayout)findViewById(R.id.scroll_container);
linear.setBackgroundColor(0xABCDEF);
//for (int i = 0; i < 10; i++) {
TextView tv = new TextView(getApplicationContext());
tv.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
));
tv.setTextSize(25);
//tv.setId(1);
tv.setTextColor(0x000000);
tv.setPadding(10, 10, 10, 10);
tv.setText("I am serial " + 0 + "\n" + "is there something more");
linear.addView(tv);
TextView tv1 = new TextView(getApplicationContext());
tv1.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
));
tv1.setTextSize(25);
//tv.setId(1);
tv1.setTextColor(0x000000);
tv1.setPadding(10, 10, 10, 10);
tv1.setText("I am serial 3 " + 1 + "\n" + "is there something more");
linear.addView(tv1);
//tv.append("my name is apple " + 0 + "\n");
TextView tv5 = new TextView(getApplicationContext());
tv5.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
));
tv5.setTextSize(25);
//tv.setId(1);
tv5.setTextColor(0x000000);
tv5.setPadding(10, 10, 10, 10);
tv5.setText("Linear Layout e" + 0 + "\n");
mainLinear.addView(tv5);
// }
}
catch (Exception e)
{
e.printStackTrace();
t1.setText("<><>"+e);
}
我的布局XML文件:
<LinearLayout
android:id="@+id/mainLinear">
<TextView android:text="Word: Monkey Kaka"
android:textSize="25dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/t1"
android:textColor="#000000"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/ScrollView">
<LinearLayout
android:id="@+id/scroll_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:text="Test 1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test 2"
android:textSize="25dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test 3"
android:textSize="25dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test 4"
android:textSize="25dp"
/>
</LinearLayout>
感谢您的帮助。
ScrollView
小部件只能承载一个直接子项。
您的布局应如下所示:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/scroll_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
然后从你的代码中你可以添加 TextView
s 到 LinearLayout
:
LinearLayout scrollContainer = (LinearLayout) findViewById(R.id.scroll_container);
scrollContainer.addView(tv);
你应该看看LogCat
。它可能会告诉你:
java.lang.IllegalStateException: ScrollView can host only one direct child
来自 ScrollView:245
source code:
@Override
public void addView(View child) {
if (getChildCount() > 0) {
throw new IllegalStateException("ScrollView can host only one direct child");
}
super.addView(child);
}
编辑:(见第三条评论)
这就是当您执行 mainLinear.addView(tv1);
时会发生的情况。
您可以在 Android Studio 布局 Preview
.
res/layout/scroll_test.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"
android:padding="10dp"
android:layout_margin="10dp"
android:orientation="vertical"
tools:context="me.ibtehaz.greapp.showWord"
android:id="@+id/mainLinear">
<TextView android:text="Word: Monkey Kaka"
android:textSize="25dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/t1"
android:textColor="#000000"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/ScrollView">
<LinearLayout
android:id="@+id/scroll_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test 1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test 2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test 3" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test 4" />
</LinearLayout>
现在,如果您将 ScrollView
布局高度设置为 wrap_content
,您将看到那 4 个 TextView
出现。
与其说是代码问题,不如说是UI设计问题。
我明白了。在所有情况下,我都为背景提供了错误的 hexValue。我将布局参数从 ViewGroup 更改为线性(我不知道为什么,在教程中找到它)。最后我使用了一个 TextView 数组,而不是重复创建一个对象。
感谢您的帮助:)
TextView t1 = (TextView)findViewById(R.id.t1);
LinearLayout mainLinear = (LinearLayout)findViewById(R.id.mainLinear);
try {
LinearLayout linear = (LinearLayout)findViewById(R.id.scroll_container);
linear.setBackgroundColor(0xABCDEFFE);
TextView []tv = new TextView[10];
for (int i = 0; i < 10; i++) {
tv[i] = new TextView(getApplicationContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
tv[i].setLayoutParams(params);
tv[i].setTextSize(25);
tv[i].setId(i);
tv[i].setTextColor(Color.BLACK);
tv[i].setPadding(10, 10, 10, 10);
tv[i].setBackgroundColor(Color.WHITE);
tv[i].setText("I am serial 4 " + i + "\n" + "is there something more");
linear.addView(tv[i]);
}
}
catch (Exception e)
{
e.printStackTrace();
t1.setText(value+"<><>"+e);
}