TextView go off screen in RelativeLayout

前端 未结 3 771
难免孤独
难免孤独 2021-01-24 16:59

Here is the layout:





        
相关标签:
3条回答
  • 2021-01-24 17:01

    You can avoid all of this by giving each textview a width of match_parent and an appropriate layout_weight. The below code will give you exactly what you need. Enjoy!

    <?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>
    
    0 讨论(0)
  • 2021-01-24 17:01

    The answer is easy. Just 3 lines of XML code. In your textview, place the following lines:

    <TextView
      android:layout_width = "match_parent"
      android:layout_height = "wrap_content"
      android:width = "0dp"/>
    
    0 讨论(0)
  • 2021-01-24 17:19

    This is because your textView width is wrap_content. So means if your text is too long then it will be out of screen. So better way is to set it to match_parent.

    And Yes if it is too long then you can set MaxLines for it.

    0 讨论(0)
提交回复
热议问题