2014-10-07 70 views
1

我想要一个干净整洁的形式,像这样:如何使用TextView和EditText高效地创建表单?

Name:   ____________ 
Foo:   ____________ 
Bar:   ____________ 
Whatever:  ____________ 

每个线路共享屏幕高度相等(在这种情况下,全屏幕的25%),并且每个TextView的应该是对的左半屏幕和EditText应该从屏幕的中心开始。

我目前的解决方案是(本质):

<LinearLayout android:orientation="vertical"> 
    <LinearLayout android:orientation="horizontal" weight="1"> 
     <TextView weight="1"> 
     <EditView weight="1"> 
    </LinearLayout> 

    <LinearLayout android:orientation="horizontal" weight="1"> 
     <TextView weight="1"> 
     <EditView weight="1"> 
    </LinearLayout> 

    <LinearLayout android:orientation="horizontal" weight="1"> 
     <TextView weight="1"> 
     <EditView weight="1"> 
    </LinearLayout> 

    <LinearLayout android:orientation="horizontal" weight="1"> 
     <TextView weight="1"> 
     <EditView weight="1"> 
    </LinearLayout> 
</LinearLayout> 

这种运作良好,但创建嵌套的权重,这是不好的表演。 API级别8有没有更好的方法来做到这一点?

+0

您是否尝试过使用表格布局? – Chitrang 2014-10-07 19:54:57

+0

是的,但我无法获得每个TextView和EditView填充50%的宽度。 – user1032613 2014-10-07 20:04:37

+0

表格布局基本上是一样的东西。我建议你担心表现时,表现让你担心。否则,有一种方法可以在2中拆分relativelayout:http://stackoverflow.com/a/22362494/671543 – njzk2 2014-10-07 20:12:30

回答

1

您可以使用网格布局,很容易使用,它会压扁你的看法(通过删除所有不必要的LinearLayout)

<GridLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:columnCount="2" 
    android:rowCount="4"> 

    <TextView 
     android:layout_column="0" 
     android:layout_row="0"/> 

    <EditView 
     android:layout_column="1" 
     android:layout_row="0"/> 

     ....  
</GridLayout> 

详情看一看Android dev doc

+0

好的解决方案,但GridLayout被添加到API级别14.尽管有upvote! – user1032613 2014-10-07 20:08:47

0

如果你想你可以右对齐EditText和leftAlign TextView,以便在布局中获得对齐的组件。然后你可以从右向左输入文本。如果您想要使用嵌套权重,这是一种潜在的方法。它可能不会得到你所问的风格类型。

在场上进一步玩minWidth填写Dialog可能会提供一个干净的结构。只要猜测你想从邮件中完成什么以及你的整体概念如何。

相关问题