我创建了这个名为 searchfragment 的 fragment 。下面是搜索 fragment 的布局 XML 文件。不幸的是,设计并不像我希望的那样流畅。
这是我想要的样子: 第一行:欢迎文字(有效) 第二行:搜索输入字段(也有效) 第三行(这里变得复杂了):应该有一个名为“搜索”的文本,然后是一个带有您可以搜索的项目(例如名称、年份、价格)的微调框。然后是搜索按钮。接下来是您可以对输出进行排序。因此,有一个带有“排序”的文本,一个带有 critirion 的微调器和一个排序按钮。最好的情况是搜索部分应该向左对齐,而排序部分应该向右对齐,但仍然居中。 第四行:输出字段。 现在,由于所有元素都出现在我的设备上,因此它们不在 Eclipse 的图形布局中。我已经尝试了几个小时让它工作,但如果你不搜索你的对象,那就有点困难了。
长文本 - 短任务:请帮助我优化我的设计。
搜索 fragment .xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:layout_gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:text="@string/welcome" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/edit_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:hint="@string/edit_message" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|center_horizontal"
android:layout_marginTop="8dp" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/search_for" />
<Spinner
android:id="@+id/criterion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:entries="@array/searcharray"
android:prompt="@string/prompt" />
<Button
android:id="@+id/btnSubmit"
android:layout_width="136dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@drawable/android"
android:onClick="sendMessage"
android:text="@string/button_send" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sort_for"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<Spinner
android:id="@+id/sort"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:entries="@array/sortarray"
android:prompt="@string/prompt" />
</LinearLayout>
<Button
android:id="@+id/sort_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@drawable/android"
android:onClick="sortAgain"
android:text="@string/button_send" />
</LinearLayout>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/showResults"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
</ScrollView>
</LinearLayout>
我添加了当前设计的照片。周围有红色框的空间是第三“行”,应该更改。第二个微调器(我不知道为什么)太宽了。它应该和第一个一样宽。然后所有元素的高度应该大致相同,并且元素应该居中。
最佳答案
您的布局方向正确,但第二个 Spinner
占用如此多空间的原因是您将其 layout_weight
设置为 1 ,并且由于它是唯一具有 layout_weight
的布局,因此它占用了剩余的可用空间。
如果您希望项目在屏幕上占据一定的空间,那么您可以坚持使用 layout_weight
并为所有项目赋予该属性。
例如,对于第三行,您可以去掉 Spinner
周围的 LinearLayout
,更改一些 layout_width
属性,然后为您的所有项目指定一个 layout_weight
。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|center_horizontal"
android:layout_marginTop="8dp" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/search_for" />
<Spinner
android:id="@+id/criterion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:entries="@array/searcharray"
android:prompt="@string/prompt" />
<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@drawable/android"
android:onClick="sendMessage"
android:text="@string/button_send" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/sort_for"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="@+id/sort"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:entries="@array/sortarray"
android:prompt="@string/prompt" />
<Button
android:id="@+id/sort_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@drawable/android"
android:onClick="sortAgain"
android:text="@string/button_send" />
</LinearLayout>
这将为您的所有项目赋予相同的权重,因此 TextViews
的大小将与 Spinners
的大小相同,而 Spinners
的大小将与 按钮
。尝试使用 layout_weight
属性使它们更具相关性,例如,如果您希望 Buttons
的大小是 Spinners
的两倍,请设置他们的 layout_weight
为 2。
关于android - 如何优化此布局 XML 文件以符合我的想法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14849531/