【发布时间】:2018-04-22 18:13:27
【问题描述】:
我正在尝试创建一个包含多个项目的 Activity,其中之一是一个 LinearLayout,它在运行时填充了其他视图。
为此目的,我的 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:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.ibosca.thub.QuestionResultsActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/questionIcon"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:src="@drawable/ic_help_black_24dp"
android:tint="@android:color/holo_orange_dark"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/questionText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:gravity="center"
android:paddingBottom="8dp"
android:textColor="@android:color/black"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/questionIcon"
app:layout_constraintVertical_bias="0.02" />
<android.support.v7.widget.LinearLayoutCompat
android:id="@+id/responses"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintTop_toBottomOf="@id/questionText"
app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
我正在使用此 Java 代码填充响应 LinearLayoutCompat:
LinearLayoutCompat rootLayout = findViewById(R.id.responses);
ArrayList<Response> orderedresponses = question.getOrderedResponses();
for (int i = 0; i < orderedresponses.size(); i++){
currentResponse = orderedresponses.get(i);
if(i == 0){
nextResponse = orderedresponses.get(i+1);
}
View child = getLayoutInflater().inflate(R.layout.response, null);
TextView responseText = child.findViewById(R.id.responseText);
TextView votesText = child.findViewById(R.id.votesText);
responseText.setText(currentResponse.getText());
String votesInfo = getResources().getString(R.string.votesPercentQuestionResult, Integer.toString(currentResponse.getVotes()) , Float.toString(currentResponse.getPercentVote()));
//votesText.setText(currentResponse.getVotes() + " vots · " + currentResponse.getPercentVote() +"%");
votesText.setText(votesInfo);
if(i == 0 && currentResponse.getVotes() != 0 && nextResponse.getVotes() < currentResponse.getVotes()){
//Set star icon
ImageView icon = child.findViewById(R.id.icon);
icon.setImageDrawable(getResources().getDrawable(R.drawable.ic_stars_black_24dp));
}
rootLayout.addView(child);
}
而且,最后一段代码中使用的响应 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="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<ImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/icon"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:src="@drawable/ic_insert_comment_black_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/responseText"
android:layout_width="0dp"
android:layout_height="23dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_weight=".90"
android:ellipsize="end"
android:maxLines="1"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:singleLine="true"
android:textColor="@android:color/black"
android:textSize="15sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/icon"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/icon"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/votesText"
android:layout_width="0dp"
android:layout_height="23dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_weight=".90"
android:ellipsize="end"
android:maxLines="1"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:singleLine="true"
android:textSize="15sp"
app:layout_constraintBottom_toBottomOf="@+id/icon"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/icon"
app:layout_constraintTop_toBottomOf="@id/responseText" />
</android.support.constraint.ConstraintLayout>
问题:
在我的尝试中,我意识到在 Response 的 LinearLayoutCompat 之后有一个额外的空白空间,这会在实际上不需要滚动时导致滚动。
我无法弄清楚这个空间是从哪里来的,但似乎这个空白空间是随着响应线性布局长大的,所以当响应线性布局里面的内容很小时,空白空间很小,随着它的增长,空白也随之增加。
我的目标是删除这个额外的空白空间,并且仅在需要时才具有滚动视图,即当线性布局大于屏幕尺寸时。
我该如何解决?
编辑:问题的屏幕截图
正如您在第一个屏幕截图中看到的那样,内容适合屏幕,但是 android 在线性布局的底部添加了额外的空间,从而导致了一个陌生的空白空间。
【问题讨论】:
标签: android android-linearlayout android-scrollview