【问题标题】:TableRow not wrapping TextViewTableRow 不包装 TextView
【发布时间】:2014-07-06 01:05:04
【问题描述】:

TableLayout 在每一行都有一个TextViewEditText。当EditText 有多行时,TableRow 将其包装,但当TextEdit 时,它不会..

简化布局:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:shrinkColumns="*" >

    <TableRow>

        <TextView
            android:id="@+id/textView1"
            android:text="Lorem ipsum dolor sit amet, consectetur" />

        <EditText
            android:id="@+id/textView2"
            android:text="Foo" />
    </TableRow>

    <TableRow>

        <TextView
            android:id="@+id/textView3"
            android:text="Foo" />

        <EditText
            android:id="@+id/textView4"
            android:text="Lorem ipsum dolor sit amet, consectetur" />
    </TableRow>

</TableLayout>

渲染:

但当两个控件都是TextEdit 时,它会正确呈现。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:shrinkColumns="*" >

    <TableRow>

        <TextView
            android:id="@+id/textView1"
            android:text="Lorem ipsum dolor sit amet, consectetur" />

        <TextView
            android:id="@+id/textView2"
            android:text="Foo" />
    </TableRow>

    <TableRow>

        <TextView
            android:id="@+id/textView3"
            android:text="Foo" />

        <TextView
            android:id="@+id/textView4"
            android:text="Lorem ipsum dolor sit amet, consectetur" />
    </TableRow>

</TableLayout>

呈现为:

我应该如何让 Android 正确呈现第一个布局?

Ps:我已经尝试给TableRowTextViewEditText控件添加android:layout_width="fill_parent",android:layout_height="wrap_content"属性,没有任何改变。

【问题讨论】:

    标签: android android-layout android-tablelayout


    【解决方案1】:

    我遇到了同样的问题,解决所有问题的方法是为我想要自动换行的文本视图设置权​​重。

    我的两分钱...

    【讨论】:

    • 荒谬:D:D:D
    • 哇,它确实有效!不知何故,重量克服了(毫无疑问是副作用)TextView 中的一个错误。下面是那些有兴趣将 TextView 的 xml 布局添加到您的命令:android:layout_weight="1"。是的,它奏效了。
    【解决方案2】:
    <?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="vertical" >
    
        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
    
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:weightSum="4" >
    
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="1dp"
                    android:layout_weight="1"
                    android:background="#B0B0B0" />
    
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="1dp"
                    android:layout_weight="1"
                    android:background="#B0B0B0" />
    
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="1dp"
                    android:layout_weight="1"
                    android:background="#B0B0B0" />
    
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#B0B0B0" />
            </TableRow>
    
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="1dp"
                android:orientation="vertical"
                android:weightSum="4" >
    
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="1dp"
                    android:layout_weight="1"
                    android:background="#B0B0B0" />
    
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="1dp"
                    android:layout_weight="1"
                    android:background="#B0B0B0" />
    
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="1dp"
                    android:layout_weight="1"
                    android:background="#B0B0B0" />
    
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#B0B0B0" />
            </TableRow>
    
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="1dp"
                android:orientation="vertical"
                android:weightSum="4" >
    
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="1dp"
                    android:layout_weight="1"
                    android:background="#B0B0B0" />
    
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="1dp"
                    android:layout_weight="1"
                    android:background="#B0B0B0" />
    
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="1dp"
                    android:layout_weight="1"
                    android:background="#B0B0B0" />
    
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#B0B0B0" />
            </TableRow>
    
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="1dp"
                android:orientation="vertical"
                android:weightSum="4" >
    
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="1dp"
                    android:layout_weight="1"
                    android:background="#B0B0B0" />
    
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="1dp"
                    android:layout_weight="1"
                    android:background="#B0B0B0" />
    
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="1dp"
                    android:layout_weight="1"
                    android:background="#B0B0B0" />
    
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#B0B0B0" />
            </TableRow>
    
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="1dp"
                android:orientation="vertical"
                android:weightSum="4" >
    
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="1dp"
                    android:layout_weight="1"
                    android:background="#B0B0B0" />
    
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="1dp"
                    android:layout_weight="1"
                    android:background="#B0B0B0" />
    
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="1dp"
                    android:layout_weight="1"
                    android:background="#B0B0B0" />
    
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#B0B0B0" />
            </TableRow>
    
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="1dp"
                android:orientation="vertical"
                android:weightSum="4" >
    
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="1dp"
                    android:layout_weight="1"
                    android:background="#B0B0B0" />
    
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="1dp"
                    android:layout_weight="1"
                    android:background="#B0B0B0" />
    
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="1dp"
                    android:layout_weight="1"
                    android:background="#B0B0B0" />
    
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#B0B0B0" />
            </TableRow>
        </TableLayout>
    
    </LinearLayout>
    

    这些灰色单元格 r 文本框

    【讨论】:

    • 那么这个答案和问题有什么关联呢?
    • 只需在其中输入文本值,您就会发现如何在 tablerows 中呈现文本
    • 正如我在问题中提到的,当两个控件都是 TextEdit 时,它会正确呈现。
    • 我尝试一一添加xml属性..只需将 android:background="#B0B0B0" 添加到 EditText 即可使android呈现正确的布局。奇怪..
    【解决方案3】:

    向 TableLayout 尝试添加

     android:stretchColumns="*"
    

    如果 tat 也不起作用,请尝试添加权重

                    <TableRow
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="2" >
    
    
    And to the TextView and EditText add 
                  android:layout_weight="1"          
    

    【讨论】:

      【解决方案4】:

      android:layout_gravity="center_vertical" 添加到TextView 或将android:background="#B0B0B0" 添加到EditText 会强制android 正确渲染。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-31
        • 2015-02-28
        • 2011-09-16
        • 1970-01-01
        • 2011-10-05
        • 1970-01-01
        • 2020-10-04
        • 2012-02-02
        相关资源
        最近更新 更多