【问题标题】:Multiline textview getting cut off in height多行文本视图在高度上被切断
【发布时间】:2017-02-22 22:23:40
【问题描述】:

我在水平LinearLayout 中的ImageButton 旁边有一个TextView,然后将其包裹在垂直LinearLayout 中。为了让按钮显示出来,我必须在文本视图和按钮上设置一个layout_weight,并且按钮是两者中更强的。然而,这似乎使按钮决定了它们的高度,即使文本需要更高。有没有办法解决这个问题?

<LinearLayout
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    android:layout_width="wrap_content">


    <LinearLayout
        android:orientation="horizontal"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textColor="@color/text_color"
            android:textSize="@dimen/medium_font"
            android:layout_gravity="center"
            android:text="A long text that wraps correctly but then gets cut off"
            android:layout_weight="1" />

        <ImageButton
            android:contentDescription="@string/share_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/share_button"
            android:padding="5dp"
            android:layout_weight="0"
            android:layout_gravity="center"
            android:scaleType="center"
            android:background="@android:color/transparent"
            android:src="@mipmap/ic_menu_share_holo_dark"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/button_background"
        android:id="@+id/distances_parent">
        <Spinner
            android:layout_margin="5dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:entries="@array/distance_labels"
            android:id="@+id/distances" />
    </LinearLayout>

    <Space
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp" />

    <FrameLayout
        android:background="@color/back_end"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        ...
    </FrameLayout>
</LinearLayout>

这就是它的样子:

如您所见,按钮决定了高度,而不是文字。

这就是我想要的样子:

【问题讨论】:

  • 可以使用RelativeLayout 代替LinearLayout 来实现这一点。请提供您的预期输出为图像[绘图]。
  • 我已经用我想要的样子编辑了帖子。
  • 你有一个相当复杂的布局。我建议把它拆掉,看看你是否能让核心部分——如屏幕截图所示——正常工作。您也可以在第二个 LinearLayout 中尝试 XML 属性 android:measureWithLargestChild。

标签: android layout textview


【解决方案1】:

您可以使用以下代码。

   <RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_gravity="center"
        android:layout_toLeftOf="@+id/share_button"
        android:text="A long text that wraps correctly but then gets cut off"
        android:textColor="@color/text_color"
        android:textSize="@dimen/medium_font"" />

    <ImageButton
        android:id="@+id/share_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_gravity="center"
        android:layout_weight="0"
        android:background="@android:color/transparent"
        android:contentDescription="imagebutton"
        android:padding="5dp"
        android:scaleType="center"
        android:background="@android:color/transparent"
        android:src="@mipmap/ic_menu_share_holo_dark"/>

    <Spinner
        android:id="@+id/distances"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:layout_margin="5dp"
        android:entries="@array/distance_labels" />
</RelativeLayout>

如果您的 TextView 包含更多文本。 TextView 的高度将根据该 ImageButton 增加,并且微调器将调整。尝试检查。我希望它会帮助你。一切顺利。

【讨论】:

  • 我使用了这个解决方案,不是完全按照字面意思,而是按照要点。它不仅解决了问题,还简化了有点复杂的布局(正如一些人指出的那样)。 @Abtin Gramian 有一个类似的答案,我也可以选择,但这是第一个。
  • 谢谢。一切顺利。编码愉快。
【解决方案2】:

如果不了解整个视图的外观,就很难提供帮助。这是填满整个屏幕还是嵌套在另一个布局中?然后我们可以知道这个特定布局中最顶层布局匹配的高度,因为您使用的是match_parent,而不是特定值。

您需要在文本视图上设置layout_weight,因为您将宽度设置为0dp 而不是wrap_content。这些齐头并进。 0dpweightSumlayout_weight 属性用于分割 LinearLayout 内的一组元素之间的剩余空白空间。

您的Spinner 小部件嵌套在LinearLayout 中,这是不必要的,因为整个视图已经在垂直LinearLayout 中,因此微调器将出现在文本和图像下方。您希望布局尽可能浅以优化性能。

我也不确定您为什么使用Space 小部件而不是设置填充、边距或使用对齐属性。

如果您确实想处理拆分空白空间,请使用:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    android:layout_width="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="1">

        <TextView
            android:id="@+id/text_view"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:text="A long text that wraps correctly but then gets cut off"/>

        <ImageButton
            android:id="@+id/share_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/text_view"
            android:layout_toEndOf="@+id/text_view"
            android:padding="5dp"
            android:layout_gravity="center"
            android:scaleType="center"
            android:background="@android:color/transparent"
            android:src="@mipmap/ic_menu_share_holo_dark"/>


    </LinearLayout>

    <Spinner
        android:id="@+id/distances"
        android:layout_margin="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/text_view"/>

</LinearLayout>

但您似乎可以在此处使用RelativeLayout

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    android:layout_width="match_parent">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="A long text that wraps correctly but then gets cut off"/>

    <ImageButton
        android:id="@+id/share_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/text_view"
        android:layout_toEndOf="@+id/text_view"
        android:padding="5dp"
        android:layout_gravity="center"
        android:scaleType="center"
        android:background="@android:color/transparent"
        android:src="@mipmap/ic_menu_share_holo_dark"/>

    <Spinner
        android:id="@+id/distances"
        android:layout_margin="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/text_view"/>

</RelativeLayout>

【讨论】:

  • 只是想补充一点,我将微调器放在线性布局中的原因是我可以添加我想要的背景。如果您将背景直接添加到微调器,则小向下箭头会因某种原因消失。虽然线性布局可能不是最好的,所以我将其改为框架布局。
【解决方案3】:
<TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textColor="@color/text_color"
        android:textSize="@dimen/medium_font"
        android:layout_gravity="center"
        android:text="A long text that wraps correctly but then gets cut off"
        android:layout_weight="1" />

你只需要设置 layout_height ="根据你的需要" 就可以了。

【讨论】:

    【解决方案4】:

    我已经在我的 xml 中尝试过这段代码并且运行良好。 我刚刚将框架布局、线性布局和空间宽度转换为 match_parent 注意:我刚刚删除了不必要的布局和图像

    但我建议为此目的使用相对布局 因为所有工作都可以在单一层次结构中完成 你将越多的布局层次越深,屏幕加载视图所需的时间就越多 如果我们可以在最低级别做到这一点,这是一个好习惯

        <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        android:layout_width="wrap_content">
    
    
        <LinearLayout
            android:orientation="horizontal"
            android:layout_height="wrap_content"
            android:layout_width="match_parent">
    
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:textColor="#000000"
                android:layout_gravity="center"
                android:text="A long text that wrapdasdsd dff f df df sdf asdf sdf sadf dsf sdf sdaf adsf adsf sdf sdaf sdf sdf sadf sadf sdf sd fsdf sadf asdf sdaf sdaf sf saf asf dsf sadf sdf sdaf sdf as df dfs correctly but then gets cut off"
                android:layout_weight="1" />
    
            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/share_button"
                android:padding="5dp"
                android:layout_weight="0"
                android:layout_gravity="center"
                android:scaleType="center"
                android:background="@android:color/transparent"
                android:src="@mipmap/ic_launcher"/>
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </LinearLayout>
    
        <Space
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="0dp" />
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
        </FrameLayout>
    </LinearLayout>
    

    【讨论】:

      【解决方案5】:

      你做错了。如果你使用 layout_weight 那么你需要将 textView 和 ImageButton 的 layout_width 都设置为零 dp。根据你的要求分配它们。像这样:

      <TextView
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:text="A long text that wraps correctly but then gets cut off"
          android:textSize="@dimen/medium_font"
          android:layout_weight="9"/>
      
      <ImageButton
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:background="@android:color/transparent"
          android:src="@mipmap/ic_menu_share_holo_dark">
      

      您可以为布局高度选择水的大小。但不要对布局宽度进行任何更改。如果可行,请在评论部分告诉我

      【讨论】:

        猜你喜欢
        • 2021-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多