Android 中页眉在顶部、页脚在底部的布局

发布于 2024-11-06 22:11:24 字数 2284 浏览 4 评论 0原文

我正在尝试创建一个我认为非常简单的 Android 屏幕布局。我基本上想要顶部有一个页眉,底部有一个页脚,以及一个填充屏幕空间其余部分的 EditText 框。页眉和 EditText 框工作正常,但我似乎无法显示页脚。这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent" 
              android:layout_height="match_parent"
              android:orientation="vertical">
    <ImageView android:id="@+id/imageView1" 
               android:src="@drawable/droidiary"
               android:layout_width="wrap_content" 
               android:layout_height="wrap_content"
               android:layout_gravity="center">
    </ImageView>
    <View android:id="@+id/view1" 
          android:layout_height="2dip"
          android:background="#FFAAAAFF" 
          android:layout_width="wrap_content">
    </View>
    <RelativeLayout android:id="@+id/rlLayout1"
                    android:layout_width="fill_parent" 
                    android:layout_height="fill_parent">
        <EditText android:inputType="text|textMultiLine"
                  android:layout_alignParentTop="true" 
                  android:hint="Type about your day..."
                  android:text="" 
                  android:id="@+id/txtEntry" 
                  android:layout_height="match_parent"
                  android:layout_width="match_parent">
        </EditText>
        <LinearLayout android:layout_alignParentBottom="true"
                      android:id="@+id/linearLayout1" 
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content">
            <View android:id="@+id/view2" 
                  android:layout_height="2dip"
                  android:background="#FFAAAAFF" 
                  android:layout_width="wrap_content">
            </View>
            <TextView android:text="TextView" 
                      android:id="@+id/textView1"
                      android:layout_width="wrap_content" 
                      android:layout_height="wrap_content">
            </TextView>
        </LinearLayout>
    </RelativeLayout>
</LinearLayout>

就像我说的,除了我的页脚之外,一切正常。谁能告诉我我做错了什么?

I am trying to create an Android screen layout that I think is pretty simple. I basically want a header at the top and a footer at the bottom, along with an EditText box that fills the rest of the screen space. The header and EditText box is working fine, but I cannot seem to get my footer to display. Here is my code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent" 
              android:layout_height="match_parent"
              android:orientation="vertical">
    <ImageView android:id="@+id/imageView1" 
               android:src="@drawable/droidiary"
               android:layout_width="wrap_content" 
               android:layout_height="wrap_content"
               android:layout_gravity="center">
    </ImageView>
    <View android:id="@+id/view1" 
          android:layout_height="2dip"
          android:background="#FFAAAAFF" 
          android:layout_width="wrap_content">
    </View>
    <RelativeLayout android:id="@+id/rlLayout1"
                    android:layout_width="fill_parent" 
                    android:layout_height="fill_parent">
        <EditText android:inputType="text|textMultiLine"
                  android:layout_alignParentTop="true" 
                  android:hint="Type about your day..."
                  android:text="" 
                  android:id="@+id/txtEntry" 
                  android:layout_height="match_parent"
                  android:layout_width="match_parent">
        </EditText>
        <LinearLayout android:layout_alignParentBottom="true"
                      android:id="@+id/linearLayout1" 
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content">
            <View android:id="@+id/view2" 
                  android:layout_height="2dip"
                  android:background="#FFAAAAFF" 
                  android:layout_width="wrap_content">
            </View>
            <TextView android:text="TextView" 
                      android:id="@+id/textView1"
                      android:layout_width="wrap_content" 
                      android:layout_height="wrap_content">
            </TextView>
        </LinearLayout>
    </RelativeLayout>
</LinearLayout>

Like I said, everything works except my footer. Can anyone tell me what I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

表情可笑 2024-11-13 22:11:24

您的布局对于您想要实现的目标来说太复杂了。我建议避免 RelativeLayout 除非你绝对必须使用它。

以下是如何使用 LinearLayout 创建页眉/内容/页脚布局的简单示例:
(请注意 content 元素中的 layout_weight

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

        <TextView                   
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:padding="5dp" 
                android:text="Header" />

        <EditText
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:singleLine="false" 
                android:hint="Type about your day..."
                android:text="" >
        </EditText>

        <TextView                   
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:padding="5dp" 
                android:text="Footer" />

</LinearLayout>        

在此处输入图像描述

Your layout is too complicated for what you want to achieve. I would recommend to avoid RelativeLayout unless you absolutely have to use it.

Here is a simple example of how to create header/content/footer layout using LinearLayout:
(note the layout_weight in the content element)

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

        <TextView                   
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:padding="5dp" 
                android:text="Header" />

        <EditText
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:singleLine="false" 
                android:hint="Type about your day..."
                android:text="" >
        </EditText>

        <TextView                   
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:padding="5dp" 
                android:text="Footer" />

</LinearLayout>        

enter image description here

少女情怀诗 2024-11-13 22:11:24

似乎有很多额外不需要的布局。这将为您布局三个区域,您可以在其中放置任何内容,或者只是将它们设为视图控件。每个区域的高度将为 25%、50%、25%。如果您不需要百分比,请删除页眉和页脚的权重并设置其宽度值,但保留 EditText 的权重。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent" 
              android:layout_height="match_parent"
              android:orientation="vertical">
   <View android:id="@+id/headerView"
         android:layout_width="match_parent"
         android:layout_height="0dip"
         android:layout_weight="2" />
   <EditText android:id="@+id/editBox"
         android:layout_width="match_parent"
         android:layout_height="0dip"
         android:layout_weight="1" />
   <View android:id="@+id/footerView"
         android:layout_width="match_parent"
         android:layout_height="0dip"
         android:layout_weight="2" />
</LinearLayout>

There seems to be a lot of extra unneeded layouts. This will layout the three areas for you and you can put anything inside them or just make them a view control. The height of each area will be 25%, 50%, 25%. If you don't want percentages, remove the weights from the header and footer and set their width value but leave the weight of the EditText.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent" 
              android:layout_height="match_parent"
              android:orientation="vertical">
   <View android:id="@+id/headerView"
         android:layout_width="match_parent"
         android:layout_height="0dip"
         android:layout_weight="2" />
   <EditText android:id="@+id/editBox"
         android:layout_width="match_parent"
         android:layout_height="0dip"
         android:layout_weight="1" />
   <View android:id="@+id/footerView"
         android:layout_width="match_parent"
         android:layout_height="0dip"
         android:layout_weight="2" />
</LinearLayout>
阪姬 2024-11-13 22:11:24

如果我正确理解布局的哪一部分是页眉和页脚,请添加以下行:

android:layout_above="@+id/linearLayout1"

到 EditText

If I correctly understood which part of your layout is header and footer, add this line:

android:layout_above="@+id/linearLayout1"

to EditText

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文