首先,让我尝试对我要在这里完成的工作进行布局。
编辑文本
EditText 搜索按钮
ListView(搜索结果。只能有一个,带有适配器和 wrap_content 高度的 ListView 似乎适用于此,有一个按钮可以将结果添加到下面的 ListView。一旦添加单击按钮此 ListView 崩溃,这正是我所追求的)
TextView(添加对象的标签)
ListView(添加的对象列表,我再次使用列表行布局适配器)
保存按钮
我本来打算粘贴我的代码,但要完成的代码太多了。我遇到的问题是 ListViews。基本上,包含添加的对象的 ListView 最终会将 SaveButton 推离屏幕。我已经尝试了这个网站和许多其他网站上列出的大量解决方案,但它们似乎都无法正常工作。
基本上,我希望 SaveButton 始终位于底部,并且我不希望它在 ListView 变得太大时被推出屏幕。我发现“有效”的唯一解决方案是显式设置 ListView 的高度。但是,这会在从平板电脑到手机(Nexus7 和 Galaxy S3)时出现问题。我认为对尺寸使用 dip 可以防止这种情况发生,但显然不是。
如果有人有创建此类布局的好策略,那就太好了。或者甚至是学习如何使用 Android 笨重的 UI 系统的好资源(它确实有一些不足之处)。
编辑:这是我尝试使用 RelativeLayout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/main_background"
android:orientation="vertical" >
<EditText
android:id="@+id/plan_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="@string/plan_name_hint"
android:textColor="@color/text_color" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/object_search_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/plan_name"
android:ems="10"
android:hint="@string/search_objects_text"
android:textColor="@color/text_color" >
</EditText>
<Button
android:id="@+id/objects_search_button"
style="@style/button_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/object_search_text"
android:layout_below="@id/plan_name"
android:layout_alignParentRight="true"
android:background="@drawable/black_button"
android:text="@string/search_objects_button_label" />
<ListView
android:id="@+id/search_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/object_search_text"
android:background="@color/main_background"
android:textColor="@color/text_color" >
</ListView>
<TextView
android:id="@+id/objects_list_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/search_result"
android:paddingBottom="8dip"
android:paddingLeft="8dip"
android:text="@string/plan_objects_list_label"
android:textColor="@color/text_color"
android:textSize="22sp"
android:textStyle="bold" />
<ListView
android:id="@+id/plan_objects"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/objects_list_label"
android:background="@color/main_background"
android:textColor="@color/text_color" >
</ListView>
<Button
android:id="@+id/save_plan_button"
style="@style/button_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/black_button"
android:paddingLeft="8dip"
android:text="@string/save_button_label" />
最佳答案
如果您认为 Android UI 系统笨拙,显然您还没有尝试去理解它。对于大多数事情,它的设计都非常好。
如果您希望某个 View (或多个 View )始终位于底部,那么您需要将屏幕设置为 RelativeLayout 并将 android:layout_alignParentBottom="true"放在这些元素上。然后添加 android:layout_above="id"在任何你想要在它们上面的地方,其中 id 是你想要在底部的元素的 id。
关于android - 用户界面布局问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18109329/