【问题标题】:Place View at bottom of a screen/layout using a TableLayout使用 TableLayout 将视图放置在屏幕/布局的底部
【发布时间】:2012-04-20 19:17:49
【问题描述】:

我试图在屏幕底部的布局中的其他视图元素下放置一个带有两个按钮的 TableRow,但如果我放在那里,TableRow 就会从屏幕上消失。我放在顶部或中间,没问题。

我尝试了很多 layout_gravity、gravity 和 weight 组合,但我无法得到它。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:gravity="center_horizontal" 
    android:text="@string/traza" 
    android:layout_margin="10sp"/>

    <TableRow
        android:id="@+id/tableRow8"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="3sp"
        android:gravity="center"
        android:weightSum="2" >

        <Button
            android:id="@+id/reiniciar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_margin="5sp"
            android:background="@drawable/bordebutton"
            android:text="@string/reiniciar" />

        <Button
            android:id="@+id/comprobar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_margin="5sp"
            android:background="@drawable/bordebutton"
            android:text="@string/comprobar" />

    </TableRow>

<android.gesture.GestureOverlayView
    android:id="@+id/gestures"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:fadeEnabled="true"
    android:fadeOffset="3000"
    android:gestureStrokeType="multiple"
    android:eventsInterceptionEnabled="true"
    android:orientation="vertical"/>
</LinearLayout>

【问题讨论】:

    标签: android android-layout android-xml gravity android-layout-weight


    【解决方案1】:

    您需要使用 RelativeLayout 来实现这一点。

    你去吧:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10sp"
            android:gravity="center_horizontal"
            android:text="@string/traza"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    
        <TableRow
            android:id="@+id/tableRow8"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="3sp"
            android:gravity="center"
            android:weightSum="2"
            android:layout_alignParentBottom="true" >
    
            <Button
                android:id="@+id/reiniciar"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="5sp"
                android:layout_weight="1"
                android:background="@drawable/bordebutton"
                android:text="@string/reiniciar" />
    
            <Button
                android:id="@+id/comprobar"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="5sp"
                android:layout_weight="1"
                android:background="@drawable/bordebutton"
                android:text="@string/comprobar" />
        </TableRow>
    
        <android.gesture.GestureOverlayView
            android:id="@+id/gestures"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:eventsInterceptionEnabled="true"
            android:fadeEnabled="true"
            android:fadeOffset="3000"
            android:gestureStrokeType="multiple"
            android:orientation="vertical" />
    
    </RelativeLayout>
    

    【讨论】:

    • 它不起作用,它崩溃了:( 04-21 17:05:06.363: E/AndroidRuntime(20964): java.lang.RuntimeException: Unable to start activity ComponentInfo{es.hsk. ap/es.hsk.ap.juega.Trazos}:java.lang.ClassCastException:android.gesture.GestureOverlayView
    • 然后从你的布局中删除android.gesture.GestureOverlayView,清理项目然后运行它。实际上,我自己并没有将 GestureOverlayView 添加到布局中,您的布局中已经提到过。如果它崩溃了,那么你是如何运行它的?
    • 正如你一开始所说,当我将 LineaLayout 更改为 RelativeLayout 时,它会崩溃。我无法删除此视图,我需要它。问题不在于它,如果你在中间使用其他布局,就会发生这种情况,比如 imageview
    • 不要简单地将LinearLayout一词替换为RelativeLayout...只需将整个xml复制到我的答案中并将其替换为您拥有的那个...
    【解决方案2】:

    编辑: 如果您想使用View 中包含的功能,您的根元素应该是TableLayout。您目前拥有的是 LinearLayout

    我没有看到任何提及非TableRow 视图如何垂直放置在documentation 中。

    根据我的经验,TableLayout 总是会将内容直接放在最后一行的下方。

    是否尝试过使用RelativeLayoutLinearLayout 来完成您的需要?它比TableLayout 灵活得多。

    您也可以关闭您的TableLayout 并在其下方放置一个LinearLayout

    【讨论】:

    • 我尝试了 LinearLayout 而不是 TableRow,但同样的问题,它上面的对象视图,使用了所有的屏幕尺寸。我想使用所有可用的屏幕,但在布局底部可以看到这两个按钮。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-30
    • 1970-01-01
    • 2015-06-24
    • 2014-11-21
    • 2011-03-06
    • 2014-04-29
    • 2018-12-05
    相关资源
    最近更新 更多