我正在将市场申请作为我大学的一个项目。因此,我正在从数据库加载产品,并且需要将它们显示在我想要使用 ScrollView 的列表中,因为我相当确定所有数据都不会在屏幕上显示一次。
我给了 ScrollView
和 ScrollView
中包含的 LinearLayout
我认为有必要包含所有这些内容的所有标签,然后开始添加 TextViews有关产品的信息并将其添加到 LinearLayout 中,但是当尝试在模拟器上运行时,LinearLayout 最底部的最后一个对象被粗略地截断中间。
在我想不出任何办法之后,我一直在寻找解决这个问题的方法,但没有任何效果,我在这里看到了使 ScrollView 的重力居中的想法,但这不起作用,center_vertical没有'要么,给它属性 android:fillViewPort="true"
没有任何作用,要么 android:layout_weight="1"
没有效果,并且 android:orientation="自从我初始化了
就一直在那里。ScrollView
以来,vertical"
所以我对此感到困惑,因为即使我尝试通过硬编码调整 LinearLayout
的大小,以便每次添加对象时扩展 1000px,这也不会执行任何操作,事实上,直到将 6000px 硬编码到布局上之前,它甚至不会以任何有意义的方式做出响应,此时它基本上只是复制自身,但没有任何内容。我不明白出了什么问题。所以我向你提出一个问题。
以下包含整个 Activity 布局的 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>
这也是将 View 添加到布局的 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();
}
}
哦,其中创建的按钮没有添加,该行被故意注释掉,我想在摆弄按钮位置之前解决 ScrollView
的问题,这也是原因目前位置分配没有任何意义。
感谢您的耐心等待。
最佳答案
输入以下代码:
<LinearLayout
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
关于java - ScrollView 内的 LinearLayout 扩展得不够远,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58890521/