Long TextViews在LinearLayout

时间:2018-05-21 20:11:56

标签: android

我在LinearLayout中有三个TextView,它们排列在另一个旁边。但是,如果TextView中间的文本变得太长,则会将左侧TextView推离屏幕。

想要使用android:weight,因为我不想为三个TextView之间的比例预定义一个修复值。

如何解决?

activity_main.xml中:

<?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" >

    <TextView
        android:id="@+id/item_left"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:text="LEFT"
        android:gravity="center"
        android:layout_gravity="left|center_vertical" />

    <TextView
        android:id="@+id/item_middle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#aaaaaa"
        android:text="This is a very very long text which is so long that it wraps but unfortunatelly also pushes the TextView with ID item_right off the screen."
        android:layout_gravity="left|center_vertical" />

    <TextView
        android:id="@+id/item_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RIGHT"
        android:gravity="center"
        android:layout_gravity="right|center_vertical" />

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

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

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/item_left"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:text="LEFT"
        android:gravity="center"
        android:layout_gravity="left|center_vertical" />

    <TextView
        android:id="@+id/item_middle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#aaaaaa"
        android:text="This is a very very long text which is so long that it wraps but unfortunatelly also pushes the TextView with ID item_right off the screen."
        android:layout_gravity="left|center_vertical" />

    <TextView
        android:id="@+id/item_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RIGHT"
        android:gravity="center"
        android:layout_gravity="right|center_vertical" />

</LinearLayout>
</ScrollView>

答案 1 :(得分:0)

尝试将weights设置为每个TextView,每个TextViewLinearLayout中拥有自己的空间,而不是向右推TextView屏幕文字将代之以换行:

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

<TextView
    android:id="@+id/item_left"
    android:layout_width="0dp"
    android:layout_height="40dp"
    android:layout_weight="1"
    android:text="LEFT"
    android:gravity="center"
    android:layout_gravity="left|center_vertical" />

<TextView
    android:id="@+id/item_middle"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="#aaaaaa"
    android:text="This is a very very long text which is so long that it wraps but unfortunatelly also pushes the TextView with ID item_right off the screen."
    android:layout_gravity="left|center_vertical" />

<TextView
    android:id="@+id/item_right"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="RIGHT"
    android:gravity="center"
    android:layout_gravity="right|center_vertical" />

  </LinearLayout>

您不需要为每个TextView等空间提供例如:TextView更多空间操纵weights例如:android:layout_weight="2" ScrollView如果文字长度大于屏幕尺寸,那么将所有内容包裹在 import requests url = "http://store.place.com.br/api/oms/pvt/orders?page=nPages&per_page=50" headers = { 'accept': "application/json", 'content-type': "application/json", 'x-vtex-api-apptoken': "{{VTEX-API-TOKEN}}", 'x-vtex-api-appkey': "{{VTEX-API-KEY}}" } response = requests.request("GET", url, headers=headers) print(response.text)中就可以防止发出不可见的文字。