【问题标题】:Android TextView - height to fill max space and ellipsizeAndroid TextView - 填充最大空间和椭圆大小的高度
【发布时间】:2013-02-19 21:55:34
【问题描述】:

我正在尝试这样的布局:

|---------------------------| |---------------------------|
| 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>  

【问题讨论】:

  • 你能提供你想要的图片吗?
  • 当您在父布局中使用 wrap_content 时,文本“应该填充标题和图像后留下的空间”是什么意思?
  • 我更新了我想要实现的布局示例。希望现在很清楚。布局高度是恒定的,但是里面的标题和图片高度不同,所以我需要省略多行文本并使其填充可用空间

标签: android android-layout textview


【解决方案1】:

好的,终于找到答案了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);
}
});

【讨论】:

【解决方案2】:
 <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>  

你需要这样的结构,我希望你能解释它。

【讨论】:

  • 感谢您的帮助,刚刚尝试过 - 但文本仍然没有省略号:/
  • 请把修改后的xml文件内容贴出来,我会编辑的
  • 尝试了上面的布局。文本没有省略。它超出了可用空间
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-23
  • 1970-01-01
  • 2015-04-24
  • 1970-01-01
  • 1970-01-01
  • 2014-08-12
相关资源
最近更新 更多