【问题标题】:Adding a Layout to the bottom of the screen将布局添加到屏幕底部
【发布时间】:2015-06-24 18:18:09
【问题描述】:

我有以下 XML 布局。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip" >

    <TextView
        android:id="@+id/itemNumber"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="6dip"
        android:textSize="50px"/>

    <TextView
        android:id="@+id/secondLine"
        android:layout_width="fill_parent"
        android:layout_height="26dip"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@id/itemNumber"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/firstLine"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/secondLine"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_alignWithParentIfMissing="true"
        android:layout_toRightOf="@id/itemNumber"
        android:gravity="center_vertical"
        android:textSize="16sp" />

    <RelativeLayout 
        android:id="@+id/InnerRelativeLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <Button 
            android:text="Submit" 
            android:id="@+id/Button"
            android:layout_alignParentRight="true" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </Button>

        <EditText 
            android:id="@+id/EditText" 
            android:layout_width="fill_parent"
            android:layout_toLeftOf="@id/Button"
            android:layout_height="wrap_content">
        </EditText>

    </RelativeLayout>

</RelativeLayout>

我需要做的是让编辑文本和按钮出现在屏幕底部。我正在使用 3 个文本视图显示为列表视图。所以我可以从列表中选择一个并将其输入到编辑文本中以提交值。

但我不能让它们出现在底部。我该怎么做?

【问题讨论】:

标签: android android-layout


【解决方案1】:

替换

    android:layout_height="?android:attr/listPreferredItemHeight"

    android:layout_height="match_parent"

// 看看这个布局:

<?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"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="textView 1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="textView 2" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="textView 3" />
    </LinearLayout>

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

        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="submit" />
    </LinearLayout>

</RelativeLayout>

【讨论】:

  • 成功了,谢谢...但我需要在顶部有这 3 个文本视图。现在那些 3 已经延伸到底部。有什么建议吗?
  • 你想如何垂直或水平对齐你的文本视图。
  • 获取标题视图@k9yosh ....检查我的答案,然后..您只需在滚动视图容器中放置一个标题布局..nd 将这个 android:layout_below="@id/header" >
  • @k9yosh,我已经更新了我的答案,如果你不介意你可以试试这个布局,它看起来正是你想要的(如果我没记错的话)。您只需要根据您的要求进行修改即可。
【解决方案2】:

我有一个这样的工作示例..希望它会有所帮助

   <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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" >

     <!-- Header aligned to top -->

      <!-- Footer aligned to bottom -->
     <LinearLayout
       android:id="@+id/footer"
        android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_alignParentBottom="true"
       android:weightSum="1"
       android:gravity="center" >
       <Button
                android:id="@+id/button_show_contact"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Info." />

         </LinearLayout>

       <!-- Scrollable Content below header and above footer -->

      <ScrollView
        android:id="@+id/container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/footer" >

      <LinearLayout
        android:id="@+id/liimage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
     </LinearLayout>
     </ScrollView>   
</RelativeLayout>

如果您有任何问题,请联系我

【讨论】:

    【解决方案3】:

    只需添加一行代码:

    android:gravity="bottom"
    

    编辑:更容易将第二个布局更改为:LinearLayout

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多