【问题标题】:Android Studio limited scrolling sizeAndroid Studio 限制滚动大小
【发布时间】:2020-10-31 01:34:47
【问题描述】:

我目前正在 Android Studio 上开发一个应用程序,但我遇到了滚动视图问题。我动态创建按钮(按钮的数量等于我的数据库中的按钮数量),但我无法滚动到页面底部并查看每个按钮。我不知道这个问题是由 ScrollView 还是由 ScrollView 内部的 LinearLayout 引起的。这是 XML 和它的 Java 代码: XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="@+id/layoutgroup"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gradient_list"
    tools:context=".GroupList">


    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="134dp"
        android:fontFamily="@font/cartoon"
        android:text="Artists"
        android:textColor="#FFFFFF"
        android:textSize="40dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/back"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/back"
        android:layout_width="0dp"
        android:layout_height="68dp"
        android:layout_marginStart="11dp"
        android:layout_marginEnd="67dp"
        app:layout_constraintEnd_toStartOf="@+id/textView3"
        app:layout_constraintHorizontal_bias="0.135"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:srcCompat="@drawable/back2" />

    <ScrollView
        android:id="@+id/listG"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_marginLeft="1dp"
        android:fillViewport="true"
        android:layout_marginTop="10dp"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/back">

        <LinearLayout
            android:id="@+id/layoutG"
            android:layout_width="match_parent"
            android:fillViewport="true"
            android:layout_height="wrap_content"
            android:orientation="vertical" />
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

Java(动态创建按钮的循环):

while(db.selectArtist(i).size()>0){
            List<String> data = db.selectArtist(i);
            Log.i("Artiste",String.valueOf(data));
            Button bouton = new Button(getApplicationContext());
            bouton.setX(160);
            bouton.setY(1 * i + 60);
            bouton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            layoutG.addView(bouton,LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            bouton.setText(data.get(0));
            bouton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent start = new Intent(getApplicationContext(), start.class);
                    start.putExtra("NomArtiste", String.valueOf(bouton.getText()));
                    start.putExtra("IdArtiste", String.valueOf(data.get(1)));
                    startActivity(start);
                    finish();
                }
            });
            i=i+1;
            listB.add(i + 1);
        }

这是结果 image 感谢您的帮助,这是我在这里的第一篇文章,而且我是 Java 编码新手!

【问题讨论】:

    标签: java android xml android-layout


    【解决方案1】:

    您需要使用滚动视图包装整个布局,因为仅包装线性布局(在您的代码上下文中)将使布局可滚动,但不会完全包含其子视图。 考虑使用以下布局:(根据您的舒适度调整一些值)

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView 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:id="@+id/your-desired-view-name"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="1dp"
    android:layout_marginTop="10dp"
    android:background="your-desired-background-here"
    android:fillViewport="true"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/back"
    tools:context="your-desired-context-here">
    
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_marginEnd="134dp"
            android:fontFamily="@font/cartoon"
            android:text="Artists"
            android:textColor="#FFFFFF"
            android:textSize="40dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/back"
            app:layout_constraintTop_toTopOf="parent" />
    
        <ImageView
            android:id="@+id/back"
            android:layout_width="0dp"
            android:layout_height="68dp"
            android:layout_marginStart="11dp"
            android:layout_marginEnd="67dp"
            app:layout_constraintEnd_toStartOf="@+id/textView3"
            app:layout_constraintHorizontal_bias="0.135"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0"
            app:srcCompat="@drawable/back2" />
    
        <LinearLayout
            android:id="@+id/layoutG"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fillViewport="true"
            android:orientation="vertical" />
    </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>
    

    【讨论】:

      猜你喜欢
      • 2022-09-23
      • 2023-03-06
      • 2011-04-13
      • 1970-01-01
      • 2013-04-05
      • 2011-02-07
      • 2012-12-21
      • 2014-06-17
      • 2014-06-12
      相关资源
      最近更新 更多