TextView在扩展时会挤出另一个而不是包裹

时间:2017-09-25 11:56:40

标签: android textview android-linearlayout

我正在尝试为Android应用程序制作聊天视图,但我有一个问题,聊天泡沫挤压消息时间,直到它完全折叠它,然后它包装到下一行。 这是xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginTop="4dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="left|center"
    android:orientation="horizontal"
    android:weightSum="1">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.8"
        android:gravity="left|center"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/chat_levi"
            android:padding="10dp"
            android:text="Insert text here"
            android:textColor="@color/blueGray50"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/time"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="bottom|left"
            android:text="18:30"
            android:textSize="14sp" />

    </LinearLayout>

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="0.2" />

</LinearLayout>

图片展示了它的样子: enter image description here

这就是它包装时的样子: enter image description here

我还在聊天中为其他用户设置了另一个布局,对他来说,我只是将所有重力左移改为权利,并交换了一些元素的位置,如上图所示(它的代码非常相似,所以我排除了它,但是如果有人要求它,我会添加它)。对于那个,我得到了预期的效果。

enter image description here

是否有某种方法可以让第一个与镜像方式相同?

3 个答案:

答案 0 :(得分:0)

请尝试以下代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="4dp"
    android:layout_marginTop="4dp">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left|center"
        android:orientation="horizontal">

        <FrameLayout
            android:layout_weight="0.8"
            android:layout_width="0dp"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/content"
                android:layout_height="wrap_content"
                android:background="@drawable/chat_levi"
                android:padding="10dp"
                android:layout_width="wrap_content"
                android:text="Insert texaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaat here"
                android:textColor="@color/blueGray50"

                android:textSize="16sp" />
        </FrameLayout>


        <TextView
            android:id="@+id/time"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="bottom|left"
            android:text="18:30"
            android:textSize="14sp" />
        <View
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0.2" />
    </LinearLayout>
</RelativeLayout>

upd:最后固定间距

答案 1 :(得分:0)

可能有一百万种方法可以对这只猫进行修饰,但是你已经使用权重进行线性布局,你可以为你的文本视图做一些非常相似的事情:

float

你可以使用权重来为你的时间戳提供更多或更少的空间。

EDIT ******

另一种为这只猫设置皮肤的方法就是将maxwidth放在你的xml

 <TextView
            android:id="@+id/content"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:padding="10dp"
            android:text="Insert ioadjfojdajidijfaseidfijoajsdifjasdijfijsadpfaosdjiofipasjdifapsjdfjasdjfasidjfasdjfoipjoijoij here"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/time"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="bottom|left"
            android:text="18:30"
            android:textSize="14sp" />

但是,您必须为不同的布局管理它。

答案 2 :(得分:0)

我想出了一个解决方案。因为镜像的工作完美无缺,所以我最终只用[{1}}镜像了RelativeLayout的“镜像”,然后我在android:scaleX="-1"元素上使用了android:scaleX="-1"

以下是代码:

TextView

最终结果:

enter image description here

enter image description here