问题界面效果重现
就像上图显示那样,如果在标题位置(使用了android:ellipsize="end")出现了省略符号,那这个item下面就会有多余的空行。现在是想它和没有出现省略符号的一样,在日期下方有正常的间距。当前代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingStart="20dp"
android:paddingEnd="20dp">
<TextView
android:id="@+id/headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:lines="1"
android:maxLines="1"
android:ellipsize="end"
android:textSize="18sp"
android:text="测试文字测试文字测试文字测试文字测试文字测试文字测试文字"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dp"
android:paddingStart="20dp"
android:paddingEnd="20dp">
<TextView
android:id="@+id/author"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:singleLine="true"
android:lines="1"
android:maxLines="1"
android:ellipsize="end"
android:textSize="16sp"
android:textAlignment="textStart"
android:text="测试文字"/>
<TextView
android:id="@+id/issuetime"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textAlignment="textEnd"
android:text="2021-06-05"/>
</LinearLayout>
</LinearLayout>
问题发现:在隐藏标题所在LinearLayout后,发现其下方的LinearLayout的宽度不能占满,此时“时间”所在TextView会进行换行显示,而在加上标题一起显示时,这个LinearLayout又能占满宽度。目前还不知道是什么原因,可能是TextView认为父控件还是没有占满宽度,而自己字符数加上“作者”的字符数超过了实际占有宽度,所以就自己换行。
目前解决方法:将内层的第二个LinearLayout替换成RelativeLayout,并设置好内部控件的相对布局,就不会产生多余空行了。
修改后的代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingStart="20dp"
android:paddingEnd="20dp">
<TextView
android:id="@+id/headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:lines="1"
android:maxLines="1"
android:ellipsize="end"
android:textSize="18sp"
android:text="测试文字测试文字测试文字测试文字测试文字测试文字测试文字"/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:paddingStart="20dp"
android:paddingEnd="20dp">
<TextView
android:id="@+id/author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:lines="1"
android:maxLines="1"
android:ellipsize="end"
android:textSize="16sp"
android:textAlignment="textStart"
android:layout_alignParentStart="true"
android:layout_toStartOf="@+id/issuetime"
android:text="测试文字"/>
<TextView
android:id="@+id/issuetime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textAlignment="textEnd"
android:layout_alignParentEnd="true"
android:text="2021-06-05"/>
</RelativeLayout>
</LinearLayout>
修改后的效果图: