android - 允许 TextView 在屏幕之外

标签 android xml android-layout android-activity

我希望我的 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>

最佳答案

尝试在您的 3 TextView 上使用此属性

android:maxLines="1"

设置 maxLines="1"后我得到了这个结果

enter image description here

关于android - 允许 TextView 在屏幕之外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31503928/

相关文章:

android - 如何将 facebook SDK 添加到 android 项目?

Android:在xml中将字符设为粗体

java - 关于 Java 到 XML 转换的问题,反之亦然

android - 在 gridview 之上叠加 ImageView

android - 统一安卓 : How to preload scenes in cache

android - 复古贴合响应与 Pro Guard 崩溃

C#:检查 XML 中的重复元素

android - 检查 ExpandableListView 中的 Firstvisible item 是 Groupindicator?

android - 强制重新布局 View 组,包括其所有子项

android - "Can' t 在 child() 中为参数 'pathString' 传递空值”以及其他 Proguard 和 Firebase 问题