【问题标题】:Buttonbar won't stick to bottom of screen按钮栏不会粘在屏幕底部
【发布时间】:2011-04-21 07:25:44
【问题描述】:

我正在尝试将我创建的按钮栏放在每个屏幕的底部。我很容易成功地完成了第一个屏幕。

现在我已经尝试将它放到其他屏幕上,但它似乎无法粘在屏幕底部。当我查看 hiearchyviewer 时,它看起来像包裹在我的布局和按钮栏周围的 RelativeLayout,并没有填满整个屏幕,但是,它的高度设置为填充父项。

谁能帮我指出我哪里出错了?

这是我使用的 XML:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<RelativeLayout android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:orientation="vertical">
    <TableLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:padding="5pt" android:id="@+id/table"
        android:stretchColumns="1">
        <TableRow>
            <TextView android:text="@string/Arbeiderbediende"
                android:id="@+id/txtArbeiderBediende" android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </TableRow>

        <TableRow android:gravity="center">
            <RadioGroup android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:orientation="horizontal"
                android:id="@+id/group1">

                <RadioButton style="@style/RadioButton"
                    android:layout_width="wrap_content" android:id="@+id/rbArbeider"
                    android:text="@string/Arbeider" android:layout_height="wrap_content"
                    android:paddingLeft="18pt" />

                <RadioButton style="@style/RadioButton"
                    android:layout_width="wrap_content" android:id="@+id/rbBediende"
                    android:text="@string/Bediende" android:layout_height="wrap_content"
                    android:layout_marginLeft="20pt" android:paddingLeft="18pt" />
            </RadioGroup>
        </TableRow>

        <TableRow android:gravity="center" android:paddingTop="5dip">
            <TextView android:text="@string/OverzichtFuncties"
                android:id="@+id/txtFuncties" android:layout_width="0dip"
                android:layout_weight="1" android:layout_height="wrap_content" />

            <Spinner android:layout_height="wrap_content" style="Spinner"
                android:layout_width="0dip" android:layout_weight="2"
                android:id="@+id/cmbFuncties" />
        </TableRow>

        <TableRow android:gravity="center" android:paddingTop="5dip">
            <TextView android:text="@string/Regio" android:id="@+id/txtRegio"
                android:layout_width="0dip" android:layout_weight="1"
                android:layout_height="wrap_content" />

            <Spinner android:layout_height="wrap_content"
                android:layout_width="0dip" android:layout_weight="2" android:id="@+id/cmbRegio" />
        </TableRow>

        <TableRow android:gravity="center" android:paddingTop="5dip">
            <TextView android:text="@string/Opleiding" android:id="@+id/txtOpleidingsniveau"
                android:layout_width="0dip" android:layout_weight="1"
                android:layout_height="wrap_content" />

            <Spinner android:layout_height="wrap_content"
                android:layout_width="0dip" android:layout_weight="2"
                android:id="@+id/cmbOpleidingsniveau" />
        </TableRow>

        <Button android:text="@string/VindJobButton" android:id="@+id/btnFindJob"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:layout_marginTop="10dip" />


    </TableLayout>

    <stage.accent.Toolbar android:layout_width="fill_parent"
        android:layout_height="70dip" android:layout_alignParentBottom="true"
        android:layout_below="@+id/table" />
</RelativeLayout>

这是我取得的结果,也是我想要得到的结果

【问题讨论】:

    标签: android xml


    【解决方案1】:

    如果您希望您的按钮栏始终在屏幕底部可见并且不随内容滚动,您可能需要切换您的 RelativeLayoutScrollView 标记,并将您的按钮栏移动到第一个子项你的RelativeLayoutScrollView 是第二个:

    <?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">
        <stage.accent.Toolbar android:id="id/buttonBar" android:layout_width="fill_parent"
            android:layout_height="70dip" android:layout_alignParentBottom="true" />
        <ScrollView android:layout_above="@+id/buttonBar"
            android:layout_width="fill_parent" android:layout_height="fill_parent">
            <TableLayout android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:padding="5pt" android:id="@+id/table"
                android:stretchColumns="1">
                <TableRow>
                    <TextView android:text="@string/Arbeiderbediende"
                        android:id="@+id/txtArbeiderBediende" android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
                </TableRow>
                <TableRow android:gravity="center">
                    <RadioGroup android:layout_width="fill_parent"
                        android:layout_height="wrap_content" android:orientation="horizontal"
                        android:id="@+id/group1">
    
                        <RadioButton style="@style/RadioButton"
                            android:layout_width="wrap_content" android:id="@+id/rbArbeider"
                            android:text="@string/Arbeider" android:layout_height="wrap_content"
                            android:paddingLeft="18pt" />
    
                        <RadioButton style="@style/RadioButton"
                            android:layout_width="wrap_content" android:id="@+id/rbBediende"
                            android:text="@string/Bediende" android:layout_height="wrap_content"
                            android:layout_marginLeft="20pt" android:paddingLeft="18pt" />
                    </RadioGroup>
                </TableRow>
                <TableRow android:gravity="center" android:paddingTop="5dip">
                    <TextView android:text="@string/OverzichtFuncties"
                        android:id="@+id/txtFuncties" android:layout_width="0dip"
                        android:layout_weight="1" android:layout_height="wrap_content" />
    
                    <Spinner android:layout_height="wrap_content" style="Spinner"
                        android:layout_width="0dip" android:layout_weight="2"
                        android:id="@+id/cmbFuncties" />
                </TableRow>
                <TableRow android:gravity="center" android:paddingTop="5dip">
                    <TextView android:text="@string/Regio" android:id="@+id/txtRegio"
                        android:layout_width="0dip" android:layout_weight="1"
                        android:layout_height="wrap_content" />
    
                    <Spinner android:layout_height="wrap_content"
                        android:layout_width="0dip" android:layout_weight="2" android:id="@+id/cmbRegio" />
                </TableRow>
                <TableRow android:gravity="center" android:paddingTop="5dip">
                    <TextView android:text="@string/Opleiding" android:id="@+id/txtOpleidingsniveau"
                        android:layout_width="0dip" android:layout_weight="1"
                        android:layout_height="wrap_content" />
    
                    <Spinner android:layout_height="wrap_content"
                        android:layout_width="0dip" android:layout_weight="2"
                        android:id="@+id/cmbOpleidingsniveau" />
                </TableRow>
                <Button android:text="@string/VindJobButton" android:id="@+id/btnFindJob"
                    android:layout_width="fill_parent" android:layout_height="wrap_content"
                    android:layout_marginTop="10dip" />
            </TableLayout>
        </ScrollView>
    </RelativeLayout>
    

    这样你就可以实现你想要的:

    按钮栏将始终位于屏幕底部。

    并且通过滚动内容,您也可以看到它的最后一点,它不会隐藏在按钮栏后面:

    【讨论】:

    • 不过,我有一个问题。如果我的内容比屏幕大怎么办。我已经实现了ScrollView,所以滚动不会有问题。但是屏幕的最后一点,例如提交按钮,将在栏之后隐藏。你是怎么解决的?
    • 上面的示例使按钮栏始终可见(它不会随内容滚动)。还是我误会你了,提交按钮不在按钮栏中?
    • 是的,我希望按钮栏像上面显示的那样固定,然后有一个按钮,例如,它位于内容屏幕的底部。然后它隐藏在按钮栏后面。
    • 不,它没有隐藏!滚动视图在按钮栏开始的地方结束 (android:layout_above="@id/buttonBar"),您可以滚动到它。查看我的更新(最后一张截图)。
    • 非常感谢!我错过了那条线!
    【解决方案2】:

    我仍然建议不要使用 ScrollView 这里是一个如何在底部显示 ButtonBar 的示例

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content" android:layout_height="wrap_content">
    
        <RelativeLayout
            android:id="@+id/headerlayout" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            >
    
            <Button android:id="@+id/amap_back_btn" android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true" android:text="Back">
            </Button>
            <TextView  android:id="@+id/amap_txt"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:text="" android:layout_centerVertical="true"
                android:layout_centerHorizontal="true">
            </TextView>
            <ImageButton android:id="@+id/amap_action_btn"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:src="@drawable/icon_menu_action"
                android:layout_alignParentTop="true" android:layout_alignParentRight="true">
            </ImageButton>
        </RelativeLayout>
    
        //What ever widgets you wants to add
    
        <LinearLayout 
        android:id="@+id/bottomlayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:orientation="horizontal"
    
        >
    
            <ImageButton android:id="@+id/map_refresh_btn"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:src="@drawable/icon_refresh" 
                android:gravity="center"
                >
            </ImageButton>
            <Button android:id="@+id/map_log_btn" android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:text="Log"
                android:gravity="center">
            </Button>
            <Button android:id="@+id/map_map_btn" android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:text="Map"
                android:gravity="center"
                >
            </Button>
        </LinearLayout>
    
    </RelativeLayout>
    

    这就是最强大的解决方案。

    如果你觉得有用,别忘了把它标记为答案。

    【讨论】:

    • 为什么不使用 ScrollViews?如果我的内容更大(在某些屏幕上更大,Android 不会让它自己滚动。
    • 你可以添加一个 ListView 来代替评论或滚动视图
    【解决方案3】:

    请采用一种主要的线性布局 滚动视图上方和结束后 滚动视图请设置您的底部 酒吧..

    这也是我的建议。 LinearLayout 是主视图。 在里面

    • 另一个 LinearLayout(layout_height=0dp 和 layout_weight = 1)*ll_container*
    • 还有你的标签栏

    *ll_container*地方

    • 带有表格布局的滚动视图(layout_height=0dp 和 layout_weight = 1)
    • 和按钮

    可能是为了解释。使用 layout_height=0dp 和 layout_weight = 1 您可以在放置标签栏和放置按钮后为布局提供整个空闲空间

    UPD

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
        <LinearLayout android:layout_width="fill_parent" 
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="vertical">
            <ScrollView android:layout_width="fill_parent"
                android:layout_height="0dp" 
                android:layout_weight="1">
                <TableLayout android:layout_width="fill_parent"
                    android:layout_height="fill_parent" android:padding="5pt" android:id="@+id/table"
                    android:stretchColumns="1">
                    <TableRow>
                        <TextView android:text="@string/Arbeiderbediende"
                            android:id="@+id/txtArbeiderBediende" android:layout_width="wrap_content"
                            android:layout_height="wrap_content" />
                    </TableRow>
    
                    <TableRow android:gravity="center">
                        <RadioGroup android:layout_width="fill_parent"
                            android:layout_height="wrap_content" android:orientation="horizontal"
                            android:id="@+id/group1">
    
                            <RadioButton style="@style/RadioButton"
                                android:layout_width="wrap_content" android:id="@+id/rbArbeider"
                                android:text="@string/Arbeider" android:layout_height="wrap_content"
                                android:paddingLeft="18pt" />
    
                            <RadioButton style="@style/RadioButton"
                                android:layout_width="wrap_content" android:id="@+id/rbBediende"
                                android:text="@string/Bediende" android:layout_height="wrap_content"
                                android:layout_marginLeft="20pt" android:paddingLeft="18pt" />
                        </RadioGroup>
                    </TableRow>
    
                    <TableRow android:gravity="center" android:paddingTop="5dip">
                        <TextView android:text="@string/OverzichtFuncties"
                            android:id="@+id/txtFuncties" android:layout_width="0dip"
                            android:layout_weight="1" android:layout_height="wrap_content" />
    
                        <Spinner android:layout_height="wrap_content" style="Spinner"
                            android:layout_width="0dip" android:layout_weight="2"
                            android:id="@+id/cmbFuncties" />
                    </TableRow>
    
                    <TableRow android:gravity="center" android:paddingTop="5dip">
                        <TextView android:text="@string/Regio" android:id="@+id/txtRegio"
                            android:layout_width="0dip" android:layout_weight="1"
                            android:layout_height="wrap_content" />
    
                        <Spinner android:layout_height="wrap_content"
                            android:layout_width="0dip" android:layout_weight="2" android:id="@+id/cmbRegio" />
                    </TableRow>
    
                    <TableRow android:gravity="center" android:paddingTop="5dip">
                        <TextView android:text="@string/Opleiding" android:id="@+id/txtOpleidingsniveau"
                            android:layout_width="0dip" android:layout_weight="1"
                            android:layout_height="wrap_content" />
    
                        <Spinner android:layout_height="wrap_content"
                            android:layout_width="0dip" android:layout_weight="2"
                            android:id="@+id/cmbOpleidingsniveau" />
                    </TableRow>
                </TableLayout>
            </ScrollView>
            <Button android:text="@string/VindJobButton" android:id="@+id/btnFindJob"
                android:layout_width="fill_parent" android:layout_height="wrap_content"
                android:layout_marginTop="10dip" />
        </LinearLayout>
        <stage.accent.Toolbar android:layout_width="fill_parent"
            android:layout_height="70dip" android:layout_alignParentBottom="true"
            android:layout_below="@+id/table" />
    </LinearLayout>
    

    【讨论】:

    • 在@Chirag 的指导下,我设法用相对布局和没有权重来做到这一点。它有什么不同吗?我想以正确的方式去做。
    • 我认为,您可以同时使用这两种解决方案。我又读了你的问题。而且我知道您只希望按钮栏始终可见。而且我不喜欢真正的相对布局......我只是对它们有太多问题
    猜你喜欢
    • 1970-01-01
    • 2015-03-05
    • 1970-01-01
    • 1970-01-01
    • 2020-05-24
    • 2012-05-22
    • 2020-11-28
    • 2013-07-14
    • 1970-01-01
    相关资源
    最近更新 更多