【问题标题】:Scrollview won't show everything滚动视图不会显示所有内容
【发布时间】:2018-08-31 00:13:46
【问题描述】:

我正在动态地将视图添加到滚动视图(线性布局)中,它不会显示所有内容。例如,如果我添加 10 个视图(TextView 和 EditView),它将显示为 4,如果我添加更多,例如 200,它将达到 131,但我可以看到下一个视图的开头Like this:

这是 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);
}

其他一切似乎都正常。 有什么想法吗?

【问题讨论】:

    标签: android scrollview programmatically-created


    【解决方案1】:

    将此属性添加到您的滚动视图:

         android:fillViewport="true"
    

    你的身高也是“0dp”。我知道您为什么要这样做,但一定要检查它所限制的视图,并确保有足够的空间来显示您要显示的内容。

    【讨论】:

    • 我添加了属性,但它根本不影响:/
    【解决方案2】:

    所以我不知道为什么,但似乎如果我在循环之外添加一些东西,一旦完成工作,滚动视图通常会停止,就会显示丢失的内容。

    新代码是:

    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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-16
      • 1970-01-01
      • 1970-01-01
      • 2019-06-09
      • 2020-08-21
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      相关资源
      最近更新 更多