Android 多行文本视图在高度上被截断

Android 多行文本视图在高度上被截断,android,layout,textview,Android,Layout,Textview,我在水平LinearLayout中的TextView旁边有一个ImageButton,然后将其包装在垂直LinearLayout中。为了让按钮显示出来,我必须在文本视图和按钮上设置一个布局权重,按钮是两个按钮中最强的一个。然而,这似乎使按钮决定了两者的高度,即使文本需要更高。有没有办法绕过这个问题 <LinearLayout android:orientation="vertical" android:layout_height="match_parent" and

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

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



您只需设置layout_height=“根据您的需要”即可

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

您需要在文本视图上设置
layout\u weight
,因为您将宽度设置为
0dp
,而不是
wrap\u content
。这些是齐头并进的。
0dp
weightSum
layout\u weight
属性用于在
线性布局中的一组元素之间分割剩余的空白

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

我也不清楚为什么要使用
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>

我已经在xml中尝试了这段代码,并且工作得非常完美。 我刚刚转换了框架布局、线性布局和空间宽度以匹配父对象 注意:我刚刚删除了不必要的布局和图像

但是我建议使用相对布局来达到这个目的 因为所有的工作都可以在一个等级内完成 您在Hirachi级别的布局中放置的越深,屏幕加载视图所需的时间就越多 如果我们能在最低限度内做到这一点,这是一种良好的做法

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

你做错了。如果你使用的是布局权重,那么你需要将textView和ImageButton的布局宽度都设置为零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">


您可以选择版面高度的宽度。但不要对版面宽度进行任何更改。如果这样做有效,请在评论部分告诉我,而不是使用线性布局。您可以使用RelativeLayout来实现这一点。请以图像[drawing]的形式提供您的预期输出。我已经用我想要的外观编辑了这篇文章。您的布局相当复杂。我建议把它拆开,看看你是否能让核心部分——如你的屏幕截图所示——正常工作。您还可以在第二行布局中尝试XML属性android:measureWithLargestChild。它不仅解决了问题,还简化了有点复杂的布局(正如一些人指出的)@Abtin Gramian有一个类似的答案,我也可以选择,但这是第一个。谢谢。祝你一切顺利。快乐的编码。只是想补充一点,我在线性布局中使用微调器的原因是,我可以添加我想要的背景。如果将背景直接添加到微调器,则小向下箭头会因某种原因而消失。虽然线性布局可能不是最好的,所以我将其改为框架布局。
    <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>
<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">