允许TextView超出屏幕范围

4

我希望我的TextView可以在水平方向上允许超出屏幕(同时禁用水平滚动条),就像这样:

enter image description here

然而,我现在的XML会导致它自动适应父元素大小,就像这样:

enter image description here

代码:

<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/parentlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff">

    <TextView
        android:layout_marginTop="30px"
        android:id="@+id/tab1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello1"
        android:textColor="#1089F9"
        android:textSize="40sp"
        android:layout_marginLeft="20dp"
        android:layout_marginBottom="5dp"/>

    <TextView
        android:layout_marginTop="30px"
        android:id="@+id/tab2"
        android:layout_toRightOf="@+id/tab1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello2"
        android:textColor="#1089F9"
        android:textSize="40sp"
        android:layout_marginLeft="50dp"
        android:layout_marginBottom="5dp"/>

    <TextView
        android:layout_marginTop="30px"
        android:id="@+id/tab3"
        android:layout_toRightOf="@+id/tab2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello3"
        android:textColor="#1089F9"
        android:textSize="40sp"
        android:layout_marginLeft="50dp"
        android:layout_marginBottom="5dp"/>

</RelativeLayout>

我该如何更改我的代码,使布局显示为第一张图片中的样式?如果TextView超出父视图,只需将其裁剪掉即可。

编辑:尝试过LinearLayout,但也没有效果

<LinearLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/parentlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    android:orientation="horizontal">

    <TextView
        android:layout_marginTop="30px"
        android:id="@+id/tab1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello1"
        android:textColor="#1089F9"
        android:textSize="40sp"
        android:layout_marginLeft="20dp"
        android:layout_marginBottom="5dp"/>

    <TextView
        android:layout_marginTop="30px"
        android:id="@+id/tab2"
        android:layout_toRightOf="@+id/tab1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello2"
        android:textColor="#1089F9"
        android:textSize="40sp"
        android:layout_marginLeft="50dp"
        android:layout_marginBottom="5dp"/>

    <TextView
        android:layout_marginTop="30px"
        android:id="@+id/tab3"
        android:layout_toRightOf="@+id/tab2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello3"
        android:textColor="#1089F9"
        android:textSize="40sp"
        android:layout_marginLeft="50dp"
        android:layout_marginBottom="5dp"/>

</LinearLayout>

android:textView上的android:singleLine="true"可能会解决问题。 - DRY Believer
1
没错!非常简单的解决方案!请将其发布为答案。这可能也会帮助其他人。另外,顺便说一下,它在截断文本后显示“...”。我该如何防止这种情况发生? - Jay
在截断文本后,它会显示“....”。我该如何停止这种情况? - Jay
Android在onDraw中剪切文本视图,不考虑clipChildren等标志。您可能需要添加一些填充等来显示所有内容。 - JohanShogun
1
@JohanShogun 填充?你能提供一个例子吗?thstalker提供的解决方案可行,但现在它在隐藏文本的位置显示一些“...”。就差一步了! - Jay
2个回答

1
尝试在您的三个TextView上使用此属性。
android:maxLines="1"

在将maxLines设置为“1”后,我得到了以下结果。

enter image description here


1
顺便问一下,这个也适用于左侧吗?我的意思是,如果我要切掉Hello1,那么这个方法也可以用吗? - Jay
1
左侧视图无法正常工作 :( 如果设置了marginRight,我想切掉hello1 - Jay
这只是一个hack,因为它只确保所有内容都在一行中显示,并隐藏剩余部分,所以对于左侧不起作用。你能告诉我你想要实现什么效果吗?为什么你想要切割第一个textView? - Sanjeev

0
使用LinearLayout而不是RelativeLayout,並將方向設置為水平。
編輯完成
    <LinearLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/parentlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/tab1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello1"
        android:textColor="#1089F9"
        android:textSize="40sp"
        android:maxLines="1"
        android:layout_marginLeft="20dp"
        android:layout_marginBottom="5dp"/>

    <TextView
        android:layout_marginTop="30px"
        android:id="@+id/tab2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello2"
        android:textColor="#1089F9"
        android:textSize="40sp"
        android:maxLines="1"
        android:layout_marginLeft="50dp"
        android:layout_marginBottom="5dp"/>

    <TextView
        android:layout_marginTop="30px"
        android:id="@+id/tab3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello3"
        android:textColor="#1089F9"
        android:textSize="40sp"
        android:maxLines="1"
        android:layout_marginLeft="50dp"
        android:layout_marginBottom="5dp"/>
</LinearLayout>

我担心如果使用LinearLayout,我将无法使用android:layout_toRightOf="@+id/tab1"属性。有没有办法绕过这个问题? - Jay
看起来LinearLayout也不能创建我想要的结果。仍然将子项适配到父视图中。 - Jay
如果您将方向设置为水平,则视图将自动设置为右侧。 - Sachin Chandil
你尝试过水平方向吗? - Sachin Chandil
我没有尝试过那个,我应该用它替换我的RelativeLayout吗? - Jay

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接