2013-02-13 15 views
1

我已经创建了这个名为searchfragment的片段。下面是searchfragment的布局XML文件。不幸的是,设计并不像我想要的那样流畅。如何优化此布局XML文件以符合我的想法?

这里就是我想要它看起来像: 第一行:欢迎文本(的作品) 第二行:搜索输入字段(这样的作品,太) Thirt行(这里情况就变得复杂):应该有一个名为“搜索”的文本,然后是一个可以搜索的项目(例如名称,年份,价格)的微调器。然后是搜索按钮。接下来的事情是你可以对输出进行排序。因此,有一个文本“排序”,一个微调批评和排序按钮。最好的情况下,搜索部分应该对齐左侧,排序的权利,但仍居中。 第四行:输出字段。 现在,由于所有元素都显示在我的设备上,因此它们不在Eclipse的图形布局中。我已经尝试了好几个小时来让它工作,但是如果你没有找到你的对象,这很难。

长文本 - 短任务:请帮我优化我的设计。

searchfragment.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> 

我已经添加了当前设计的照片。周围有红框的空间是第三个“行”,应该改变。第二个微调是(我不知道为什么)太宽。它应该和第一个一样广泛。那么所有的元素应该是相同的高度,元素应该居中。

Current state

+2

这将有助于张贴布局的图像你会喜欢实现。 – kdroider 2013-02-13 08:48:49

+0

完成。感谢你的帮助。 – chrissik 2013-02-13 09:06:37

回答

1

你在正确的轨道上你的布局上,但你的第二个Spinner是占用了太多空间的原因是你已经设置它的layout_weight为1,因为它是唯一一个与一个layout_weight,它占用了免费的剩余空间。

如果您希望物品在屏幕上占用相对数量的空间,那么您可以坚持使用layout_weight并为您的所有物品提供该物业。

例如,只为你的第三排,你会摆脱LinearLayoutSpinner各处,更改一些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这将是大小为Buttons一样一样的。玩的layout_weight属性,使之更相关,例如,如果你想Buttons是尺寸为Spinners两次,设置其layout_weight为2

+0

谢谢,这解决了我的问题。 – chrissik 2013-02-18 06:49:39