2016-08-29 63 views
0

我有一个简单GridLayout的Android GridLayout的fill_horizo​​ntal熄灭屏幕

<GridLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:columnCount="2" 
    android:rowCount="1" > 

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

    <EditText 
     android:inputType="text" 
     android:layout_column="1" 
     android:layout_row="0" 
     android:text="asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf" 
     android:layout_gravity="fill_horizontal"/> 
</GridLayout> 

然而,这将导致EditText延长关闭屏幕,如下面(图像从所述IDE中可以看出,但它同样的时在设备上运行)。

enter image description here

看来,EditText实际上是在屏幕的整个宽度,而不是GridLayout细胞,这就是为什么它会关闭屏幕的宽度。造成这种情况的布局出了什么问题?

+1

http://stackoverflow.com/questions/30845440/edittext-getting-out-of-gridlayout看到这个 – KrishnaJ

回答

0

我不确定为什么你需要GridView的情况下,也许你知道我更好,但你可以在这篇文章Gridview with two columns and auto resized images找到对你有用的东西。

在你的情况下,GridView有2列,每一个里面你有没有设置任何值的宽度和高度参数的视图,在这种情况下Android是除了他们的wrap_content。

当TextView和EditText具有layout_width =“wrap_content”时,它们会将它们自行扩展到一行,直到它们包裹整个内容为止。这就是为什么你要走出手机屏幕。

试试这个XML,你会看到预期的行为:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:columnCount="2" 
android:rowCount="1"> 
<TextView 
    android:layout_width="20dp" 
    android:layout_height="wrap_content" 
    android:layout_columnWeight="0.5" 
    android:layout_column="0" 
    android:text="Label with some long text event more text"/> 
<EditText 
    android:layout_width="50dp" 
    android:layout_height="wrap_content" 
    android:layout_columnWeight="1.5" 
    android:inputType="textMultiLine" 
    android:layout_column="1" 
    android:text="Some really long text here and more and more and more .... " 
    android:layout_gravity="fill_horizontal"/> 

我们在这里做的仅仅是设置为你的孩子一些硬编码值(的TextView和EditText上),对他们的Android :layout_width属性,并给它们android:layout_columnWeight属性。这将设置你的列的一些比例(玩这个值只是为了满足你的需要)。

使用EditText,我们也在做一些小事(android:inputType =“textMultiLine”),以确保您的文本将被包装在多行上。

顺便说一句:如果你正在做一些输入表单,我会建议你使用LinearLayout这样的其他ViewGroups。

谢谢任何​​问题请发表评论。

+0

谢谢你的回应。这个例子被削减以专注于这个问题,这就是为什么它是简单化的。该问题的评论中指出了解决方案,即将layout_width设置为0dp。 – jgm

+0

我不知道为什么你给我-1这个回应,当你正在做同样的东西来解决问题(设置0dp到你的宽度attr),但任何为什么我不能做任何事情。我的答案是接受否定的投票没有错 – Sniper

+0

我没有选择你的答案是正确的,因为它使用固定的宽度,它不会提供所需的结果来填充屏幕的宽度,但不会掉到边缘。 – jgm