【问题标题】:TableLayout & Scroll view stretching in layout布局中的 TableLayout 和滚动视图拉伸
【发布时间】:2016-02-12 00:05:48
【问题描述】:

我的 TableLayout 出现问题,显示的列未对齐, 并且通过看到按钮上的文本被剪切,视图变得越来越小。

XML 布局:

<?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="0dp"
    android:layout_margin="3dp"
    android:layout_weight="2"
    android:paddingTop="4dp"
    android:orientation="horizontal">
    <TextView
        android:id="@id/libStatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType = "fitXY"/>
</LinearLayout>

    <TableLayout
        android:id="@+id/headerTable"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="2dp"
        android:layout_marginLeft="2dp"
        android:stretchColumns="0,1"
        android:shrinkColumns="1">
        <TableRow android:id="@+id/headerRow"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/header_col1"
                android:textColor="@android:color/black"
                android:layout_marginLeft="1dp"
                android:gravity="center_horizontal"
                android:layout_weight="1"
                android:background="@drawable/cell_shape"
                android:text="@string/lib_dev_name" />
            <TextView
                android:id="@+id/header_col2"
                android:textColor="@android:color/black"
                android:gravity="center_horizontal"
                android:layout_weight="1"
                android:layout_marginRight="1dp"
                android:background="@drawable/cell_shape1"
                android:text="@string/lib_status" />
        </TableRow>
    </TableLayout>

    <ScrollView
        android:id="@+id/table_scroll"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="2dp"
        android:layout_marginLeft="2dp">
        <TableLayout
            android:id="@+id/table_devices"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="2dp"
            android:layout_marginLeft="2dp"
            android:stretchColumns="0,1"
            android:shrinkColumns="1" >
            <TableRow>
                <TextView
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:layout_marginLeft="1dp"
                    android:gravity="center_horizontal"
                    android:text="Col1" />
                <CheckBox
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:layout_marginRight="1dp"
                    android:gravity="center_horizontal"
                    android:text="Col2" />
            </TableRow>
        </TableLayout>
    </ScrollView>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_margin="3dp"
    android:layout_weight="2"
    android:paddingTop="4dp"
    android:orientation="horizontal">

    <Button
        android:id="@id/btnConnect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:scaleType = "fitXY"
        android:text="@string/lib_connect"/>

    <Button
        android:id="@id/btnCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:scaleType = "fitXY"
        android:text="@string/lib_cancel"/>
</LinearLayout>

这是我运行应用程序时布局的显示方式:

Screenshot of the application

请各位大侠指教。

提前谢谢你。

【问题讨论】:

    标签: android scrollview tablelayout


    【解决方案1】:

    试试这个代码:

    <TableLayout
        android:id="@+id/headerTable"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="2dp"
        android:layout_marginLeft="2dp"
        android:stretchColumns="*" >
        <TableRow 
            android:id="@+id/headerRow"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/header_col1"
                android:textColor="@android:color/black"
                android:layout_marginLeft="1dp"
                android:gravity="center"
                android:layout_weight="1"
                android:background="@drawable/cell_shape"
                android:text="@string/lib_dev_name" />
            <TextView
                android:id="@+id/header_col2"
                android:textColor="@android:color/black"
                android:gravity="center"
                android:layout_weight="1"
                android:layout_marginRight="1dp"
                android:background="@drawable/cell_shape1"
                android:text="@string/lib_status" />
        </TableRow>
    </TableLayout>
    
    <ScrollView
        android:id="@+id/table_scroll"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TableLayout
            android:id="@+id/table_devices"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="2dp"
            android:layout_marginLeft="2dp"
            android:stretchColumns="*" >
            <TableRow>
                <TextView
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:layout_marginLeft="1dp"
                    android:gravity="center"
                    android:text="Col1" />
                <CheckBox
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:layout_marginRight="1dp"
                    android:gravity="center"
                    android:text="Col2" />
            </TableRow>
        </TableLayout>
    </ScrollView>
    
    
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal>
    
        <Button
           android:id="@id/btnConnect"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:text="@string/lib_connect"/>
        <Button
           android:id="@id/btnCancel"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:text="@string/lib_cancel"/>
    </TableRow>
    

    【讨论】:

    • 您的代码更正了按钮拉伸,谢谢。但是,标题的列仍然没有与滚动视图中的tableLayout中的列对齐。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多