简体   繁体   English

如何在屏幕底部对齐这些按钮?

[英]How to I align these buttons along the bottom of the screen?

This is the UI I'm working on: 这是我正在使用的UI:

在此处输入图片说明

I'm trying to get the buttons at the bottom to be anchored to the bottom of the screen but in order to do this, It seems I need to have a hard-coded height for the 我正在尝试将底部的按钮固定在屏幕底部,但是为了做到这一点,看来我需要为屏幕设置一个硬编码的高度

<LinearLayout>
    <GridLayout></GridLayout>
    <GridLayout></GridLayout>
</LinearLayout>

What we see in the image is 305dip . 我们在图像中看到的是305dip If I increase that to eg 335dip it's aligned with the bottom correctly, but I know this isn't right. 如果我将其增加到例如335dip则它与底部正确对齐,但是我知道这是不对的。 However, if I set the LinearLayout 's height to match_parent , eg 但是,如果我将LinearLayout的高度设置为match_parent ,例如

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"

it pushes the buttons in the red box right off the bottom of the screen. 它将红色框内的按钮直接推到屏幕底部。

I need a way for the keypad to fill exactly as much space as it needs to, and keep the correct buttons anchored at the bottom of the screen at all times. 我需要一种使键盘完全填满所需空间的方法,并始终将正确的按钮固定在屏幕底部。 How can I do this? 我怎样才能做到这一点?

This is my layout definition: 这是我的布局定义:

<?xml version="1.0" encoding="utf-8"?>
<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:background="#ffffff"
    tools:context=".FullscreenActivity"
    android:orientation="vertical"
    android:paddingTop="0dip"
    android:paddingBottom="0dip">
    <TextView
        android:id="@+id/keypadOutput"
        android:layout_width="match_parent"
        android:layout_height="70dip"
        android:gravity="center_vertical"
        android:keepScreenOn="true"
        android:text="@string/dummy_content"
        android:textColor="#000000"
        android:textSize="25sp"
        android:textStyle="bold"
        android:background="@drawable/LCD"
        android:paddingLeft="15dip"
        android:layout_margin="5dip" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="305dip"
        android:orientation="horizontal">
        <GridLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:columnCount="3"
            android:orientation="horizontal">
            <!-- 12 buttons -->
        </GridLayout>
        <GridLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:columnCount="1"
            android:orientation="horizontal">
            <!-- 3 buttons -->
        </GridLayout>
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:gravity="bottom|center_horizontal"
        android:orientation="horizontal">
        <!-- 3 image buttons -->
    </LinearLayout>
</LinearLayout>

Correct Answer 正确答案

as per @Ascorbin's answer below, this was the solution: 根据下面@Ascorbin的答案,这是解决方案:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center_horizontal">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_horizontal"
        android:layout_alignParentBottom="true">
        <ImageButton
            android:id="@+id/button_keypad"
            android:layout_width="45dip"
            android:layout_height="45dip"
            android:background="@drawable/navigation_button"
            android:src="@drawable/keypad_light" />
        <ImageButton
            android:id="@+id/button_unset"
            android:layout_width="45dip"
            android:layout_height="45dip"
            android:background="@drawable/navigation_button"
            android:src="@drawable/unset_light" />
        <ImageButton
            android:id="@+id/button_logs"
            android:layout_width="45dip"
            android:layout_height="45dip"
            android:background="@drawable/navigation_button"
            android:src="@drawable/logs_light" />
    </LinearLayout>
</RelativeLayout>

I would suggest you to change 我建议你改变

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="305dip"
    android:orientation="horizontal">

to

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal">

you can add margin from top whatever you want 您可以根据需要从顶部添加边距

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:gravity="bottom|center_horizontal"
    android:layout_marginTop="50dp"
    android:orientation="horizontal">
    <!-- 3 image buttons -->
</LinearLayout>

I don't think you do yourself a favor by hardvoding layout heights. 我认为您不能通过严格要求布局高度来帮自己一个忙。 I'd suggest to wrap your whole layout in a RelativeLayout and anchor your buttons to the bottom with alighParentBottom="true" . 我建议将整个布局包装在RelativeLayout并使用alighParentBottom="true"将按钮固定在底部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM