【问题标题】:TextView go off screen in RelativeLayoutTextView 在 RelativeLayout 中关闭屏幕
【发布时间】:2014-02-14 14:57:53
【问题描述】:

这是布局:

<?xml version="1.0" encoding="utf-8"?>

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="2"
    android:rowCount="2"
    android:rowOrderPreserved="true"
    android:alignmentMode="alignMargins">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="0"
        android:layout_column="0"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Test:" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="0"
        android:layout_column="1"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="too long line too long line too long line too long line" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Test2:"
        android:paddingRight="10dp"
        android:layout_row="1"
        android:layout_column="0" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="1"
        android:layout_column="1"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Test line" />
</GridLayout>

这里是发生了什么的屏幕截图:

如何防止第二个 TextView(屏幕截图中选择的那个)越过屏幕边界?

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    您可以通过为每个 textview 指定 match_parent 的宽度和适当的 layout_weight 来避免所有这些。下面的代码将为您提供您所需要的。尽情享受吧!

    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:text="Test 1"/>
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="line too long line too long line too long line too long line too long "/>
            </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:text="Test 2"/>
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="line too long line too long line too long line too long line too long "/>
        </LinearLayout>
    
    </LinearLayout>
    

    【讨论】:

    • 这是正确的方法,但答案可能会更复杂一些,例如添加代码 sn-p 显示正确的方法。很好的解决方案;)
    • 对此我深表歉意。有点着急,但想帮忙:)
    • @TheHungryAndroider:谢谢。能否请您发布一个使用 layout_weight 的示例?
    • 答案已编辑以显示代码。请记住,layout_weight 必须与 match_parent 一起使用。如果父LinearLayout是orientation:horizo​​​​ntal,那么子的layout_weight将应用于宽度,反之亦然垂直和layout_height。
    【解决方案2】:

    答案很简单。只需 3 行 XML 代码。 在您的文本视图中,放置以下行:

    <TextView
      android:layout_width = "match_parent"
      android:layout_height = "wrap_content"
      android:width = "0dp"/>
    

    【讨论】:

    • 哇... 2 天后,您解决了我的问题... 设置滚动条,设置 maxlines,将单行设置为 false,设置权重。一切都没有帮助,宽度总是屏幕尺寸的两倍。设置好宽度后(300dp也可以),线被换成了2行!谢谢!现在我问我为什么这是将文本视图包装成多行的唯一方法(可能是因为文本以编程方式更改?)
    【解决方案3】:

    这是因为你的 textView 宽度是 wrap_content。这意味着如果您的文本太长,那么它将超出屏幕。所以更好的方法是将其设置为 ma​​tch_parent

    是的,如果它太长,那么你可以为它设置 MaxLines

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多