我正在将Views动态添加到Scrollview(LinearLayout)中,但不会显示所有内容。例如,如果我添加10个视图(TextView和EditView),它将显示为4;如果添加更多,例如200,它将达到'直到131,但我可以看到下一个视图的开始{{3 }}
这是XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
tools:context=".FirstActivity"
tools:layout_editor_absoluteY="25dp">
<TextView
android:id="@+id/textView"
android:layout_width="95dp"
android:layout_height="36dp"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ScrollView
android:id="@+id/scrollView3"
android:layout_width="362dp"
android:layout_height="0dp"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
app:layout_constraintBottom_toTopOf="@+id/button4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.47"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
android:paddingBottom="10dp">
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
这是java:
public class FirstActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
LinearLayout layout = findViewById(R.id.layout);
int i = 1;
int a = 1;
int noc = 500;
int heightTV = 100;
int heightET = 10;
int widthTV = 100;
int widthET = 200;
do{
TextView textView = new TextView(this);
textView.setId(i);
String number = Integer.toString(a)+".-";
textView.setY(heightTV);
textView.setX(widthTV);
textView.setText(number);
layout.addView(textView);
EditText editText = new EditText(this);
editText.setId(i);
editText.setHint("Example: 567-ZTY");
editText.setY(heightET);
editText.setX(widthET);
editText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
layout.addView(editText);
i++;
a++;
heightTV = heightTV + 100;
heightET = heightET + 100;
} while (i<=noc);
}
其他一切似乎都正常。 有什么想法吗?
答案 0 :(得分:0)
将此属性添加到您的scrollView中:
android:fillViewport="true"
您的身高也是“ 0dp”。我知道您为什么要这样做,但一定要检查它是否受限制的视图,并确保有足够的空间来显示您要显示的内容。
答案 1 :(得分:0)
所以我不知道为什么,但是似乎如果在工作结束后在循环外添加一些东西(通常在滚动视图停止的地方),则会显示缺少的内容。
新代码为:
for (int i = 1; i<=noc; ++i){
TextView textView = new TextView(this);
textView.setId(i);
String number = Integer.toString(a)+".-";
textView.setY(heightTV);
textView.setX(widthTV);
textView.setText(number);
layout.addView(textView);
EditText editText = new EditText(this);
editText.setId(i);
editText.setHint("Example: 567-ZTY");
editText.setY(heightET);
editText.setX(widthET);
editText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
layout.addView(editText);
a++;
heightTV = heightTV + 100;
heightET = heightET + 100;
Button button = findViewById(R.id.button4);
button.setText(Integer.toString(i));
}
heightTV = heightTV + 100;
TextView textV = new TextView(this);
textV.setHeight(heightTV);
textV.setText("");
layout.addView(textV);