Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android LinearLayout:如何正确换行较长的文本而不换行_Android_Android Layout_Android Linearlayout_Android Layout Weight - Fatal编程技术网

Android LinearLayout:如何正确换行较长的文本而不换行

Android LinearLayout:如何正确换行较长的文本而不换行,android,android-layout,android-linearlayout,android-layout-weight,Android,Android Layout,Android Linearlayout,Android Layout Weight,这是当前我的listview行布局: <?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="match_parent" android:orientation="hori

这是当前我的listview行布局:

<?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="match_parent"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="5dp"
        android:text="99.99.9999"
        android:layout_weight="0.2"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="0.6">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="This is a correct looking text"
            android:id="@+id/txtComment"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.2">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:layout_marginTop="10dp"
            android:textStyle="bold"
            android:layout_marginRight="5dp"
            android:text="-1234,56" />
    </LinearLayout>
</LinearLayout>

问题出现在id为
txtcoment
的文本视图中。对于短文本内容和包含规则换行符的文本内容,一切都很好

但是,一旦有一个没有换行符的长文本,Android仍然会包装文本,但也会改变周围线性布局的权重,从而压缩左右文本视图

如何确保对于没有换行的长文本,Android仍然尊重元素的权重

显示问题的屏幕截图:

试试这个

权重无法正常工作,因为您已将宽度设置为
wrap\u content
。将其更改为
0dp


试试这个

权重无法正常工作,因为您已将宽度设置为
wrap\u content
。将其更改为
0dp



您只需将您的
android:layout\u width=“wrap\u content”
设置为设置权重的
android:layout\u width=“0dip”

<?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="match_parent"
    android:orientation="horizontal">

    <TextView
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:layout_weight="0.2"
        android:text="99.99.9999" />

    <LinearLayout
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="0.6"
        android:orientation="vertical">

        <TextView
            android:id="@+id/txtComment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="This is a correct looking textThis is a correct looking textThis is a correct looking textThis is a correct looking textThis is a correct looking text" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="0.2">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_marginTop="10dp"
            android:gravity="right"
            android:text="-1234,56"
            android:textStyle="bold" />
    </LinearLayout>
</LinearLayout>


您只需将您的
android:layout\u width=“wrap\u content”
设置为设置权重的
android:layout\u width=“0dip”

<?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="match_parent"
    android:orientation="horizontal">

    <TextView
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:layout_weight="0.2"
        android:text="99.99.9999" />

    <LinearLayout
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="0.6"
        android:orientation="vertical">

        <TextView
            android:id="@+id/txtComment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="This is a correct looking textThis is a correct looking textThis is a correct looking textThis is a correct looking textThis is a correct looking text" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="0.2">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_marginTop="10dp"
            android:gravity="right"
            android:text="-1234,56"
            android:textStyle="bold" />
    </LinearLayout>
</LinearLayout>


问题是,布局工作正常:-)您将权重设置为20%、60%和20%,这正是最后一种情况下发生的情况


另外,当使用
linerallayout
权重时,您应该设置
android:layout\u width=“0dp”

问题是,布局工作正常:-)您将权重设置为20%、60%和20%,这正是最后一种情况下发生的情况


另外,在使用
linerallayout
权重时,应该设置
android:layout\u width=“0dp”

在根元素中:
android:weightSum=“1”


在子元素中,android:layout\u width=“0dp”在根元素中,android:weightSum=“1”


在您的子元素中,android:layout\u width=“0dp”

有更好更干净的方法来实现这一点:

<android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:padding="4dp"
    app:alignmentMode="alignBounds"
    app:columnCount="3"
    app:rowOrderPreserved="false"
    app:useDefaultMargins="true">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:text="99.99.9999"
        app:layout_gravity="fill_horizontal" />

    <TextView
        android:id="@+id/txtComment"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="This is a correct looking textThis is a correct looking textThis is a correct looking textThis is a correct looking textThis is a correct looking text"
        app:layout_columnWeight="3"
        app:layout_gravity="fill_horizontal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp"
        android:layout_marginTop="10dp"
        android:gravity="right"
        android:text="-1234,56"
        android:textStyle="bold"
        app:layout_gravity="fill_horizontal" />
</android.support.v7.widget.GridLayout>

有更好更干净的方法来实现这一点:

<android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:padding="4dp"
    app:alignmentMode="alignBounds"
    app:columnCount="3"
    app:rowOrderPreserved="false"
    app:useDefaultMargins="true">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:text="99.99.9999"
        app:layout_gravity="fill_horizontal" />

    <TextView
        android:id="@+id/txtComment"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="This is a correct looking textThis is a correct looking textThis is a correct looking textThis is a correct looking textThis is a correct looking text"
        app:layout_columnWeight="3"
        app:layout_gravity="fill_horizontal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp"
        android:layout_marginTop="10dp"
        android:gravity="right"
        android:text="-1234,56"
        android:textStyle="bold"
        app:layout_gravity="fill_horizontal" />
</android.support.v7.widget.GridLayout>

太好了,谢谢。我以前从没听说过这件事。很高兴知道阅读这里的文档:太好了,谢谢。我以前从没听说过这件事。很高兴知道,请阅读以下文档: