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

Rom*_*man 3 android textview android-layout

我正在尝试制作这样的布局:

|---------------------------| |---------------------------|
| 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 ...|
|---------------------------| |---------------------------|
Run Code Online (Sandbox Code Playgroud)

想象一下,我现在有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>  
Run Code Online (Sandbox Code Playgroud)

Rom*_*man 9

好的,终于在这里找到了答案

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);
}
});
Run Code Online (Sandbox Code Playgroud)

  • 请注意,textView.getLineHeight()并不总能为您提供所需的内容,因为[文本中的标记可能导致单个行更高或更短于此高度](http://developer.android.com/reference/android /widget/TextView.html#getLineHeight()). (2认同)