简体   繁体   English

Android textview 宽度不一样

[英]Android textview width not same

I am doing the chat system.我正在做聊天系统。 So I need to do the UI for showing the message and sent time.所以我需要做 UI 来显示消息和发送时间。
Please see the below images first.请先看下面的图片。 The red color is the text view width and blue color is sent time text view width.红色是文本视图宽度,蓝色是发送时间文本视图宽度。 Now I set the sent time is right of the text view.现在我将发送时间设置在文本视图的右侧。
I would like to set the sent time text view have a margin.我想设置发送时间文本视图有一个边距。 But you can see the text view width is not same.但是你可以看到文本视图的宽度是不一样的。 So I am not hard code the margin.所以我不是硬编码边距。 So anybody can give me some useful suggestion to me.所以任何人都可以给我一些有用的建议给我。 Thanks a lots.十分感谢。

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.60"
    android:layout_height="wrap_content"/>

<TextView
    android:id="@+id/tvMessage"
    android:layout_width="0dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="@id/guideline"
    android:layout_height="wrap_content"
    android:textColor="@color/white_color"
    android:background="@color/red_color"
    android:maxWidth="@dimen/_151sdp"
    android:textStyle="bold"
    android:minWidth="30dp"/>

<TextView
    android:id="@+id/tvDateTime"
    android:layout_width="0dp"
    app:layout_constraintBottom_toBottomOf="@id/tvMessage"
    app:layout_constraintEnd_toEndOf="@id/tvMessage"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@id/tvMessage"
    android:layout_toRightOf="@id/tvMessage"
    android:textColor="@color/chat_mag_date_time"/>

</androidx.constraintlayout.widget.ConstraintLayout>

For using a component in another you can use somethings like: CardView , FrameLayout or ConstraintLayout .要在另一个组件中使用组件,您可以使用以下内容: CardViewFrameLayoutConstraintLayout

ConstraintLayout like this:像这样的ConstraintLayout

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/msg_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/cardview_dark_background">

    <TextView
        android:id="@+id/tvMessage"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:maxWidth="151dp"
        android:minWidth="30dp"
        android:text="fdsfdsfd
dsadas
asd
asd
as"
        android:textColor="@color/black"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginEnd="8dp"
        />

    <TextView
        android:id="@+id/tvDateTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/tvMessage"
        android:layout_toRightOf="@id/tvMessage"
        android:text="1234"
        android:textColor="@color/purple_700"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/tvMessage"
        android:layout_marginStart="8dp"
        />
</androidx.constraintlayout.widget.ConstraintLayout>

Add following alignments to the TextView "tvDateTime"将以下对齐添加到 TextView "tvDateTime"

android:layout_alignBottom="@id/tvMessage"
android:layout_alignRight="@id/tvMessage"

and remove the extra alignment instructions, so your TextView code will become:并删除额外的 alignment 指令,因此您的 TextView 代码将变为:

<TextView
    android:id="@+id/tvDateTime"
    android:layout_width="wrap_content"
    android:text="12:45"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@id/tvMessage"
    android:layout_alignRight="@id/tvMessage"
    android:textColor="@color/grey"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM