【问题标题】:LinearLayout inside ScrollView does not expand far enoughScrollView 内的 LinearLayout 扩展不够远
【发布时间】:2019-11-16 12:02:50
【问题描述】:

我正在为我的大学做一个市场申请。因此,我正在从我的数据库中加载产品,并且需要将它们显示在我想使用ScrollView 的列表中,因为我相当确定所有数据都不会立即显示在屏幕上。

我在ScrollView 中给出了ScrollView 和包含的LinearLayout 所有我认为有必要包含所有这些的标签,然后开始添加带有产品信息的TextViews,并将这些添加到LinearLayout 但是当试图在模拟器上运行它时,LinearLayout 最底部的最后一个对象大致在中间被切断。

我一直在寻找解决这个问题的方法,因为我想不出任何办法,但没有任何效果,我在这里看到了让滚动视图的重心居中的想法,但没有奏效,center_vertical 没有t 要么,给它属性 android:fillViewPort="true" 什么也没做 android:layout_weight="1" 没有效果,自从我初始化 ScrollView 以来,android:orientation="vertical" 一直在那里。

所以我对此感到有些困惑,因为即使我尝试通过硬编码调整LinearLayout 的大小以在每次添加对象时扩展 1000 像素,这也无济于事,实际上直到硬编码 6000 像素到布局上,它甚至没有以任何有意义的方式响应,此时它基本上只是自我复制,但没有任何内容。我不明白出了什么问题。所以我向你提出这个问题。

以下包含整个活动布局的 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:id="@+id/parentLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/base_content"
    tools:showIn="@layout/activity_basic_list">

    <View
        android:id="@+id/bar"
        android:layout_width="match_parent"
        android:layout_height="62dp"
        android:background="@color/base_bar"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </View>

    <ToggleButton
        android:id="@+id/menu"
        android:layout_width="62dp"
        android:layout_height="62dp"
        android:background="@color/base_bar"
        android:contentDescription="@string/menu"
        android:textOff="+"
        android:textOn="-"
        android:textSize="32sp"
        app:layout_constraintStart_toStartOf="@id/bar"
        app:layout_constraintTop_toTopOf="@id/bar" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="99dp"
        android:layout_marginLeft="99dp"
        android:layout_marginTop="50dp"
        android:layout_marginEnd="99dp"
        android:layout_marginRight="99dp"
        android:backgroundTint="#000000"
        android:ems="10"
        android:hint="@string/search"
        android:inputType="text"
        android:textColor="#000000"
        android:textColorHint="#000000"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/bar" />

    <ImageButton
        android:id="@+id/home"
        android:layout_width="62dp"
        android:layout_height="62dp"
        android:layout_marginStart="177dp"
        android:layout_marginLeft="177dp"
        android:layout_marginEnd="177dp"
        android:layout_marginRight="177dp"
        android:background="@color/base_bar"
        android:contentDescription="@string/logo_desc"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@android:drawable/btn_dialog" />

    <ScrollView
        android:layout_width="409dp"
        android:layout_height="465dp"
        android:layout_marginTop="188dp"
        android:layout_weight="2"
        android:fillViewport="true"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:id="@+id/scroll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical" />
    </ScrollView>


</android.support.constraint.ConstraintLayout>

这也是将视图添加到布局的java段的代码:

for(int i = 0; i < jarray_stock.length(); i++){
                System.out.println(i);
                try {
                    JSONObject jobj = jarray_stock.getJSONObject(i);

                    TextView tv = new TextView(getApplicationContext());
//                    tv.setWidth(1000);
//                    tv.setHeight(500);
                    tv.setLayoutParams(new LinearLayout.LayoutParams(1000, 500));
//                    tv.setGravity(Gravity.CENTER_VERTICAL);
                    tv.setBackgroundColor(getColor(R.color.error_content));
                    tv.setX(0);
                    tv.setY(i * (40));

                    tv.append("\n  Name: " + jobj.getString("article_name"));
                    tv.append("\n  Fach: " + jobj.getString("article_module"));
                    tv.append("\n  ISBN: " + jobj.getString("ISBN"));
                    tv.append("\n  Zustand: " + jobj.getString("status"));
                    tv.append("\n\n  Verkäufer: " + jobj.getString("selling_user"));
                    tv.append("\n  Preis: " + jobj.getString("price"));
                    tv.append("\n");

                    tv.getLayoutParams().height = LinearLayout.LayoutParams.WRAP_CONTENT;

                    Button jB = new Button(getApplicationContext());
                    jB.setWidth(50);
                    jB.setHeight(50);
                    jB.setX(750);
                    jB.setY(225+(i*(500 + 40)));
                    jB.setTextSize(17);
                    jB.setText(">>>");
                    jB.setTextColor(Color.WHITE);
                    jB.setBackgroundColor(getColor(R.color.base_active));

//                    parent_content.setMinimumHeight((jarray_stock.length() * 40) + 2000);
                    parent_content.addView(tv);
//                    parent_content.addView(jB);
                    tv.requestLayout();

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

哦,在其中创建的 Button 没有添加,该行被故意注释掉,我想在处理按钮放置之前解决 ScrollView 的问题,这也是位置分配没有的原因目前没有任何意义。

感谢您的耐心等待。

【问题讨论】:

    标签: java android-studio android-scrollview


    【解决方案1】:

    下面的代码:

    <LinearLayout
                android:id="@+id/scroll"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" />
    

    【讨论】:

    • 试过了,还是把最后一个TextView的底部剪掉了
    • 把下边距放到线性布局
    • 使用具有不同值的 android:layout_marginBottom 标记完成此操作,但也无济于事。我也尝试过填充,但这只是让 ScrollView 内的布局停止在更高的位置
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-04
    相关资源
    最近更新 更多