多线文字视图在高度上被切断

时间:2016-10-13 17:06:33

标签: android layout textview

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

layout_weight

这就是它的样子:

Cut off text

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

这就是我想要的样子:

Text not cut off

5 个答案:

答案 0 :(得分: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增加,并且微调器将调整。试试看。我希望它会对你有所帮助。一切顺利。

答案 1 :(得分:1)

如果不了解整个视图的外观,很难提供帮助。这是填充整个屏幕还是嵌套在另一个布局?然后我们可以告诉您特定布局中最顶层布局的匹配高度,因为您使用的是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>

答案 2 :(得分:0)

<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 =&#34;根据你的需要&#34;就是这样。

答案 3 :(得分:0)

我已经在我的xml中尝试了这个代码,并且完美无缺。 我刚刚将帧布局,线性布局和空间宽度转换为match_parent 注意:我刚刚删除了不必要的布局和图像

但我会为此目的使用相对布局 因为所有的工作都可以在单一的层级进行 您将越多的hirarchi布局级别越多,屏幕加载视图所需的时间就越多 如果我们能够以最低水平做到这一点,这是一种很好的做法

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

答案 4 :(得分:0)

你做错了。如果你正在使用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">

你可以选择布局高度的浇水尺寸。但是不要对布局宽度做任何改变。如果这个工作让我知道在评论部分