![](/img/trans.png)
[英]How to make a TextView multiline programmatically in Android (Java)
[英]unable to make the ScrollView and textView (added programmatically) visible in Android
我正在開發一個Android項目,我必須從JSONArray寫入數據。 我試圖為每個數組輸入使用TextView。 所以我以前編碼在我現有的布局上添加一個scrollView並添加一個TextView init。 代碼工作完美,沒有任何錯誤,但是當我編譯它時,UI中沒有任何內容。 (在我的手機中)后來,我嘗試將另一個TextView添加到現有的LinearLayout而不是ScrollView,它也沒有工作。 我檢查了其他問題和論壇,在我看來,我已經完成/寫了他們所做的一切,我仍然找不到我的錯誤。
更新1 :我已將ScrollView添加到另一個LinearLayout中,並在其中保留另一個LinearLayout。 TextView正在ScrollView內的LinearLayout中添加。 外部LinearLayout持有另一個TextView只發布異常日志(我的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
添加到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
源代碼 :
@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);
時會發生這種情況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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.