繁体   English   中英

当文本变长时,我的应用程序将2个TextViews保持在一行上

[英]My app keeps 2 TextViews on 1 line when the text gets longer

将2个TextView排成一行,而不会丢失1个textview的字符

我尝试使用RelativeLayoutLinearLayout (因为它们是我通常使用的),但是我没有得到想要的结果。
我希望第一个TextView保持不变。 我的意思是显示完整的“第一文本textview ”-而不是第一文本...(剪切的文本),甚至丢失所有文本。

更新:当第二个textview较短时,第一个textview应坚持第二个textview。 但是,当第二个textview较长时,第一个textview将不会丢失。

这是我的XML布局的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content">

    <TextView
        android:id="@+id/second_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_marginStart="14dp"
        android:maxLines="1"
        android:text="second textviewwwwwwwww"
        android:textSize="20sp"/>

    <TextView
        android:id="@+id/first_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toStartOf="@+id/second_textview"
        android:ellipsize="end"
        android:gravity="end"
        android:maxLines="1"
        android:text="first textview"
        android:textSize="20sp"/>
</RelativeLayout>

尝试这个

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

<TextView
    android:id="@+id/second_textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/first_textview"
    android:maxLines="1"
    android:text="second textviewwwwwwwww"
    android:textSize="20sp"/>

<TextView
    android:id="@+id/first_textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:maxLines="1"
    android:text="first textview"
    android:textSize="20sp"/>

您应该使用android:layout_weight尝试LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="10">

    <TextView
        android:id="@+id/first_textview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:ellipsize="end"
        android:maxLines="1"
        android:padding="5dp"
        android:text="first textview"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/second_textview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="7"
        android:ellipsize="end"
        android:maxLines="1"
        android:padding="5dp"
        android:text="second tadkfj adfkj adflkja adfkj"
        android:textSize="20sp" />
</LinearLayout>

您的答案是水平LinearLayout 第一个元素的宽度应设置为"wrap_content" ,第二个元素的宽度应设置为"match_parent" 还将第二个元素的singleLine属性设置为true 因此,您的代码将如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <TextView
        android:id="@+id/first_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:text="first textview"
        android:textSize="20sp"/>

    <TextView
        android:id="@+id/second_textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="14dp"
        android:singleLine="true"
        android:ellipsize="end"
        android:text="second textviewwwwwwwww"
        android:textSize="20sp"/>

</LinearLayout> 

暂无
暂无

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

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