【问题标题】:How to get UI to look right如何让 UI 看起来正确
【发布时间】:2010-08-24 18:28:03
【问题描述】:

我在让 ListView 正确显示时遇到问题。目前看起来像这样,带有以下 xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@drawable/favs_main">
    <Button
        android:text="Return to Home"
        android:id="@+id/return_button"
        android:layout_width="150dp" 
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:textSize="15sp"/>
    <ListView
        android:id="@+id/favsListView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="180dp"
        android:layout_above="@id/return_button"/>
</RelativeLayout>

如果您注意到列表在屏幕下方。我希望它位于收藏夹文本下方,而不是位于返回主页按钮上方。然而,问题是我总是希望按钮显示并且列表视图只占据收藏夹文本和按钮之间的空间。文字来自背景图片,所以我不能只在其下方对齐。所以即使有 100 个项目,我仍然想显示按钮。

感谢您的帮助

【问题讨论】:

    标签: android xml user-interface


    【解决方案1】:

    如果“收藏夹”一词是RelativeLayout 的背景属性中所建议的背景图像的一部分,那么如果不使用hacky margin 或其他类似效果,您将无法对齐其下方的元素。如果要对齐单词下方的元素,请将其分隔到不同的 ImageView 中,并将 ListView 的 layout_below 设置为该 ImageView 的 id。要让一个元素在其他两个元素之间正确对齐,请结合使用 layout_above 和 layout_below。

    【讨论】:

      【解决方案2】:

      您不能将 ListView 与父母的顶部对齐并为 ListView 设置一个边距,使其位于背景文本的下方吗? 您还可以更改背景以在 ImageView 中提供文本并将 ListView 对齐到 ImageView 下方。

      【讨论】:

        【解决方案3】:

        与其尝试让持久视图始终显示在 ListView 下并对齐它(您可以这样做,请参阅其他建议),您可能想看看使用 footerView:

        http://developer.android.com/intl/de/reference/android/widget/ListView.html#addFooterView

        “添加一个固定视图以显示在列表底部。”

        请注意,如果您最终需要做的不仅仅是一个按钮,它也可以是另一种布局。

        【讨论】:

          【解决方案4】:

          这是我的列表视图,它有多个条目,文本视图和按钮固定在底部。我没有插入背景。试试这个希望它会有所帮助。

          http://www.techuv.com/layout-with-butoon-and-textview-fixed-in-bottom/

          【讨论】:

            【解决方案5】:

            您可以使用简单的 LinearLayout 并在 ListView 上使用 weight 属性:

            <?xml version="1.0" encoding="utf-8"?>
            <LinearLayout 
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:background="@drawable/favs_main">
                 <TextView 
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                 />
                 <ListView
                     android:id="@+id/favsListView"
                     android:layout_width="wrap_content"
                     android:layout_weight="1"
                     android:layout_height="wrap_content"
                     android:layout_marginTop="180dp"/>
                 <Button
                     android:text="Return to Home"
                     android:id="@+id/return_button"
                     android:layout_width="150dp" 
                     android:layout_height="wrap_content"
                     android:layout_marginTop="10dp"
                     android:layout_centerHorizontal="true"
                     android:textSize="15sp"/>
             </LinearLayout>
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2023-03-18
              • 2017-06-05
              • 1970-01-01
              • 2021-06-10
              • 1970-01-01
              • 1970-01-01
              • 2011-04-28
              • 2020-06-20
              相关资源
              最近更新 更多