【问题标题】:Android: Table layout overlaps Linear LayoutAndroid:表格布局与线性布局重叠
【发布时间】:2016-03-01 10:51:05
【问题描述】:

在谈到 android 时,我是一个相当大的菜鸟,我陷入了困境。 How it should be

我希望我的表格布局位于线性布局中的按钮下方,但它只是重叠。

最终我必须能够单击按钮以完全隐藏表格。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="eu.agp_it.agpmobilemover.OverviewActivity">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="1.0"
        android:gravity="center"
        android:id="@+id/layout_button_today"
        >
        <Button
            android:id="@+id/overview_button_today"
            android:layout_height="wrap_content"
            android:layout_weight="0.95"
            android:layout_width="0dip"
            android:textSize="20sp"
            android:text="@string/overview_today"
            android:gravity="center"/>
    </LinearLayout>

<TableLayout
    android:layout_width="20sp"
    android:layout_height="fill_parent"
    android:visibility="visible"
    >
    <TableRow>
        <TextView android:id="@+id/label_depart" android:text="@string/overview_depart_time"/>
        <TextView />
    </TableRow>

</TableLayout>
    <LinearLayout
        android:id="@+id/layout_button_upcoming"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="1.0"
        android:gravity="center"
        android:layout_above="@id/layout_button_today">
        <Button
            android:id="@+id/overview_button_upcoming"
            android:layout_height="wrap_content"
            android:layout_weight="0.95"
            android:layout_width="0dip"
            android:textSize="20sp"
            android:text="@string/overview_upcoming"
            android:gravity="center"/>
    </LinearLayout>
</RelativeLayout>

【问题讨论】:

  • 使用LinearLayout作为root或正确设置layout_above/layout_below。
  • 使用 LinearLayout 而不是 RelativeLayout 并且“fill_parent”被弃用,如果你使用 LinearLayout 那么 android:layout_height="0dp" android:layout_weight="1" in TableLayout

标签: android android-layout android-linearlayout android-tablelayout


【解决方案1】:

以两张表为

<TableRow>
    </TableRow> 

<TableRow>
        </TableRow> 

在 TableLayout 内部,其中第一个表包含您的按钮,第二个表包含您的线性布局视图。此外,如您所愿,您可以在单击按钮时根据需要隐藏第二个表格行。

【讨论】:

    【解决方案2】:

    我认为您有 2 个按钮和一个 tableLayut。其中一个按钮位于页面顶部,另一个位于页面底部。我建议了这个代码:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"       android:layout_width="match_parent"
            android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            android:paddingBottom="@dimen/activity_vertical_margin"
            tools:context="eu.agp_it.agpmobilemover.OverviewActivity">
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:weightSum="1.0"
                android:gravity="center"
                android:id="@+id/layout_button_today">
                <Button
                    android:id="@+id/overview_button_today"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.95"
                    android:layout_width="0dip"
                    android:textSize="20sp"
                    android:text="@string/overview_today"
                    android:gravity="center"/>
            </LinearLayout>
    
        <TableLayout
            android:layout_width="20sp"
            android:layout_height="fill_parent"
            android:layout_below="@+id/layout_button_today"
            android:layout_above="@+id/layout_button_upcoming"
            android:visibility="visible" >
            <TableRow>
                <TextView android:id="@+id/label_depart"
                       android:text="@string/overview_depart_time"/>
                <TextView />
            </TableRow>
    
        </TableLayout>
            <LinearLayout
                android:id="@+id/layout_button_upcoming"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:weightSum="1.0"
                android:gravity="center"
                android:layout_above="@id/layout_button_today">
                <Button
                    android:id="@+id/overview_button_upcoming"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.95"
                    android:layout_width="0dip"
                    android:textSize="20sp"
                    android:text="@string/overview_upcoming"
                    android:gravity="center"/>
            </LinearLayout>
        </RelativeLayout>
    

    【讨论】:

    • 是的,我确实有两个按钮,我忘了删除这个问题的下一个按钮。我的计划是在按下第一个按钮时显示一个表格,在按下另一个按钮时显示一个。
    • 我建议您使用 RecyclerView 或 ListView 并在单击按钮后动态添加数据。然后您可以通知您的适配器更改列表。
    【解决方案3】:

    用这个替换你的 TableLayout。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"       android:layout_width="match_parent"
        android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context="eu.agp_it.agpmobilemover.OverviewActivity">
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:weightSum="1.0"
            android:gravity="center"
            android:id="@+id/layout_button_today">
            <Button
                android:id="@+id/overview_button_today"
                android:layout_height="wrap_content"
                android:layout_weight="0.95"
                android:layout_width="0dip"
                android:textSize="20sp"
                android:text="@string/overview_today"
                android:gravity="center"/>
        </LinearLayout>
    
        <TableLayout
            android:id="@+id/my_table_layout"
            android:layout_width="20sp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/layout_button_today"
            android:visibility="visible" >
            <TableRow>
                <TextView android:id="@+id/label_depart"
                    android:text="@string/overview_depart_time"/>
                <TextView />
            </TableRow>
    
        </TableLayout>
        <LinearLayout
            android:id="@+id/layout_button_upcoming"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:weightSum="1.0"
            android:gravity="center"
            android:layout_below="@id/my_table_layout">
            <Button
                android:id="@+id/overview_button_upcoming"
                android:layout_height="wrap_content"
                android:layout_weight="0.95"
                android:layout_width="0dip"
                android:textSize="20sp"
                android:text="@string/overview_upcoming"
                android:gravity="center"/>
        </LinearLayout>
    </RelativeLayout>
    

    【讨论】:

    • 你只添加了 layout_below 吗?因为如果你这样做了,我试过了,它没有用,你的解决方案可以。谢谢!
    【解决方案4】:

    将父布局设置为LinearLayout。 方向为垂直。 还设置最初在您的布局中消失的表格布局的可见性。 在代码中,在您的点击方法中设置 Visibility 可见

    tableLayout.setVisibility(View.VISIBLE);

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_margin="10dp"
        android:layout_gravity="center">
    
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="Button"/>
    
    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="+@id/tableLayout"
        android:visibility="gone"
        android:layout_margin="10dp">
    
        <TableRow>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
               android:text="text1"/>
        </TableRow>
    
        <TableRow>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:text="text2"/>
        </TableRow>
    
        </TableLayout>
    
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="Button"/>
    
        </LinearLayout>
    

    【讨论】:

      【解决方案5】:

      你可以在代码下使用:

          <LinearLayout
              android:id="@+id/layout_button_today"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:gravity="center"
              android:weightSum="1.0" >
      
              <TextView
                  android:layout_width="0dp"
                  android:layout_height="match_parent"
                  android:layout_weight=".05" />
      
                  <Button
                      android:id="@+id/overview_button_today"
                      android:layout_width="0dip"
                      android:layout_height="wrap_content"
                      android:layout_weight=".90"
                      android:gravity="center"
                      android:text="@string/overview_today"
                      android:textSize="20sp" />
      
                  <TextView
                      android:layout_width="0dp"
                      android:layout_height="match_parent"
                      android:layout_weight=".05" />
      
      
      
          </LinearLayout>
      
       <TableLayout
              android:layout_width="20sp"
              android:layout_height="fill_parent"
              android:layout_below="@+id/layout_button_today"
              android:layout_above="@+id/layout_button_upcoming"
              android:visibility="visible" >
              <TableRow>
                  <TextView android:id="@+id/label_depart"
                         android:text="@string/overview_depart_time"/>
                  <TextView />
              </TableRow>
      
          </TableLayout>
              <LinearLayout
                  android:id="@+id/layout_button_upcoming"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:weightSum="1.0"
                  android:gravity="center"
                  android:layout_above="@id/layout_button_today">
                  <Button
                      android:id="@+id/overview_button_upcoming"
                      android:layout_height="wrap_content"
                      android:layout_weight="0.95"
                      android:layout_width="0dip"
                      android:textSize="20sp"
                      android:text="@string/overview_upcoming"
                      android:gravity="center"/>
              </LinearLayout>
      
          </RelativeLayout>
      

      【讨论】: