【问题标题】:Two tableLayout inside a linearLayout一个linearLayout里面的两个tableLayout
【发布时间】:2013-02-18 21:50:21
【问题描述】:

我会在Linearlayout 中放置两个表格布局。但是使用我的代码,我只能看到一张桌子。代码如下:

<LinearLayout 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"
tools:context=".MainActivity" >

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:stretchColumns="1" >

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" >

        <TextView
            android:id="@+text_view/tw_nome"
            android:text="@string/tw_nome" />

        <EditText
            android:id="@+edit_text/et_nome"
            android:hint="@string/et_nome"
            android:imeOptions="actionNext"
            android:singleLine="true" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" >

        <TextView
            android:id="@+text_view/tw_cognome"
            android:text="@string/tw_cognome" />

        <EditText
            android:id="@+edit_text/et_cognome"
            android:hint="@string/et_cognome"
            android:imeOptions="actionDone"
            android:singleLine="true" />
    </TableRow>
</TableLayout>

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center" >

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" >

        <TextView
            android:id="@+text_view/tw_sesso"
            android:text="@string/tw_sesso" />

        <RadioGroup
            android:id="@+radio_group/rg"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+radio_button/button_m"
                android:text="@string/button_m" />

            <RadioButton
                android:id="@+radio_button/button_f"
                android:text="@string/button_f" />
        </RadioGroup>
    </TableRow>

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center" >

        <Button
            android:id="@+button/button_salva"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button_salva" />

        <Button
            android:id="@+button/button_annulla"
            android:text="@string/button_annulla" />
    </TableRow>
</TableLayout>

怎么了?

【问题讨论】:

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


    【解决方案1】:

    并排还是垂直?

    如果您正在寻找并排,请尝试layout_weight。还要注意layout_width 的变化。

    <LinearLayout 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:orientation="horizontal"
        tools:context=".MainActivity" >
    
        <TableLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:stretchColumns="1" >
                <!-- rows -->
        </TableLayout>
    
        <TableLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:stretchColumns="1" >
                <!-- rows -->
        </TableLayout>
    </LinearLayout>
    

    更新 cmets 询问为什么要将 layout_width 设置为 0dp

    设置0dp 是使用layout_weight 时的优化。将宽度设置为 0dp 会告诉布局忽略 layout_height 并使用 layout_weight 代替,在这种情况下,它会为每个 TableLayout 提供均匀的宽度。

    我实际上问了同样的问题,如果有帮助,我得到了一些有用的答案Why is 0dp considered a performance enhancement?

    【讨论】:

    • *这是如果您的 LinearLayout 的方向是水平的。如果您的方向是垂直的,请切换宽度/高度指令。
    • 我能问你为什么用“0dp”表示宽度或高度吗?
    • 我知道这听起来很别扭,但是当你使用layout_weight 时,设置0dp 是一种优化。将宽度设置为0dp 告诉布局忽略layout_height 并改用layout_weight
    • 我问了同样的问题并在此问题上发布了一些有用的答案。看看stackoverflow.com/questions/12016781/…
    【解决方案2】:

    表格布局的宽度和高度都定义为match_parent,因此它们占用的空间与父级相同。父级中没有足够的空间让两个子级以与父级相同的大小显示。

    如何解决这个问题取决于父 LinearLayout 的方向。

    【讨论】:

      【解决方案3】:

      把LinearLayout放在a里面:

      <ScrollView/>    </ScrollView>
      

      【讨论】:

        猜你喜欢
        • 2012-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-21
        • 1970-01-01
        相关资源
        最近更新 更多