Android TextView-填充最大空间和省略号的高度
我正在尝试这样的布局:Android TextView-填充最大空间和省略号的高度,android,android-layout,textview,Android,Android Layout,Textview,我正在尝试这样的布局: |---------------------------| |---------------------------| | Title of text goes there | | Title of text goes there | |---------------------------| |---------------------------| | Image goes there, height | | Image goes there, height
|---------------------------| |---------------------------|
| Title of text goes there | | Title of text goes there |
|---------------------------| |---------------------------|
| Image goes there, height | | Image goes there, height |
| can be different | | can be different, very |
|---------------------------| | very, very different |
| Long, very long text goes | |---------------------------|
| here, and here, and here | | Long, very long text goes |
| that should fill space | | here, and here, and here |
| lef after title and ima.. | | that should fill space ...|
|---------------------------| |---------------------------|
现在想象一下,屏幕上有5个这样的线性布局,2个在上面,3个在下面,都是一致的。问题是我的@Long,very Long文本不能同时省略和填充可用空间。当我设置android:maxLines时,它可以工作,但我没有恒定的maxLines,因为图像和标题高度会发生变化
<!-- item -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip"
android:layout_weight=".3">
<!-- item title -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:id="@+id/item_3_title"
android:textSize="22sp"
android:textStyle="bold"/>
<!-- item image -->
<RelativeLayout
android:id="@+id/item_3_image_holder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical">
<ImageView
android:id="@+id/item_3_image"
android:layout_width="wrap_content"
android:layout_height="120dip"
android:scaleType="centerCrop"
android:padding="2dip" >
</ImageView>
<ProgressBar
android:id="@+id/item_3_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
</ProgressBar>
</RelativeLayout>
<!-- item text -->
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left"
android:id="@+id/item_3_text"
android:ellipsize="end"
android:textSize="18sp"/>
</LinearLayout>
你需要这样的结构,我希望你能解释它。好的,终于找到了答案
你能提供你想要的图片吗?当你在父版面中使用wrap_内容时,文本“应该填充标题和图片后的空间”是什么意思?我更新了我想要实现的版面示例。希望现在一切都清楚了。版面高度是恒定的,但其中的标题和图像高度有不同的高度,因此我需要省略多行文字并使其填满可用的空间。谢谢帮助,刚刚尝试过-但文字仍然没有省略:/n请发布xml文件内容并进行修改,我将编辑它们尝试上面的版面。文本没有省略。它超出了textView.getLineHeight()无法始终提供所需的可用空间,因为@androiddeveloper在必要时修改行距@yiati这有什么帮助?某些跨距可以比文本高或短。例如ImageSpan…@androiddeveloper您只需要确保跨度不能更高。@yiati首先,我不想这样做。我想显示它们应该显示的内容,同时根据需要截断它们。其次,我很好奇:您如何确保跨度大小完全相同?
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip"
>
<!-- item title -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:id="@+id/item_3_title"
android:textSize="22sp"
android:textStyle="bold"/>
<!-- item image -->
<RelativeLayout
android:id="@+id/item_3_image_holder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/item_3_title"
>
<ImageView
android:id="@+id/item_3_image"
android:layout_width="wrap_content"
android:layout_height="120dip"
android:scaleType="centerCrop"
android:padding="2dip" >
</ImageView>
<ProgressBar
android:id="@+id/item_3_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
</ProgressBar>
</RelativeLayout>
<!-- item text -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/item_3_text"
android:layout_below="@+id/item_3_image_holder"
android:ellipsize="end"
android:textSize="18sp"/>
</RelativeLayout>
ViewTreeObserver observer = textView.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int maxLines = (int) textView.getHeight()
/ textView.getLineHeight();
textView.setMaxLines(maxLines);
textView.getViewTreeObserver().removeGlobalOnLayoutListener(
this);
}
});