简体   繁体   English

android布局 - 顶部和底部

[英]android layout - top and bottom

使用相对布局还是线性布局,如何在布局顶部水平居中放置文本字段,在底部如何在同一行上显示4个按钮,并且它们之间具有相等的间距?

<?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" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp" >

        <requestFocus />
    </EditText>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center_horizontal" >

        <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="btn 1 " />

        <Button
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/btn1"
            android:text="btn 2 " />

        <Button
            android:id="@+id/btn3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/btn2"
            android:text="btn 3 " />

        <Button
            android:id="@+id/btn4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/btn3"
            android:text="btn 4 " />
    </RelativeLayout>

</RelativeLayout>

For the top button, you can do that using the Graphical Layout interface. 对于顶部按钮,可以使用“图形布局”界面进行操作。 Just add your textfield, and set it the following properties : 只需添加您的文本字段,并将其设置为以下属性即可:

android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"

For the bottom buttons you should add a LinearLayout with the following properties : 对于底部的按钮,您应该添加带有以下属性的LinearLayout:

android:layout_alignParentBottom="true"
android:layout_width="fill_parent"

and put your 4 buttons with android:layout_weight="1" inside. 并在其中放置android:layout_weight="1" 4个按钮。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Android!"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true"/>

    <LinearLayout
        android:layout_alignParentBottom="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="Button1"
            android:layout_weight="1" />
        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="Button2"
            android:layout_weight="1" />
        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="Button3"
            android:layout_weight="1" />
        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="Button4"
            android:layout_weight="1" />
    </LinearLayout>

</RelativeLayout>

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

相关问题 Android ImageView 上从下到上和从上到下的布局动画点击 - Android Layout Animations from bottom to top and top to bottom on ImageView click 如何在顶部和底部的Android上设置布局 - How to setup a layout on top and bottom Android Android 中顶部有页眉、底部有页脚的布局 - Layout with a header at the top and a footer at the bottom in Android Android layout_gravity在LinearLayout中的底部/顶部 - Android layout_gravity Bottom/Top in LinearLayout Android使用图像视图点击上的翻译动画,从底部到顶部和从上到下为我的相对布局设置动画 - Android animate my Relative Layout from bottom to Top and Top to Bottom using translate animation on image view click Android-从布局顶部到底部从左向右滑动动画 - Android - Slide Left To Right Animation Starting from Top of Layout to Bottom 如何在布局中将顶视图和底视图显示为常量-Android - How to display Top and bottom view as constant in a layout - android 如何在Android中为布局(顶部和底部)绘制两条水平线 - How to draw two horizontal lines for the layout(top and bottom) in android Android-布局问题-Textviews顶部居中和底部居中 - Android - Layout question - Textviews top center and bottom center 如何在android中从底部到顶部对角缩放布局动画? - How to scale animate a layout diagonally from bottom to top in android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM