gpt4 book ai didi

Android TextView - 填充最大空间和椭圆大小的高度

转载 作者:太空宇宙 更新时间:2023-11-03 11:59:10 25 4
gpt4 key购买 nike

我正在尝试这样布局:

|---------------------------| |---------------------------|
| 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 个像这样的 LinearLayout 位于屏幕上,2 个在上,3 个在下,全部均匀分布。问题是我的@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>

最佳答案

好的,终于找到答案here

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);
}
});

关于Android TextView - 填充最大空间和椭圆大小的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15224809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com