2015-10-30 79 views
0

我有一个GridLayout,但孩子们将他们的layout_width设置为match_parent,并使GridLayout的尺寸扩大到屏幕外(见图片)。GridLayout比屏幕大

我该怎么做让项目match_parent,保持屏幕内的GridLayout

enter image description here

这里是我的代码:

<LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      android:layout_margin="8dp"> 

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

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Buscar" 
        android:id="@+id/tvRecorrido" 
        android:layout_gravity="center_vertical"/> 

       <SearchView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/svBusqueda" 
        android:queryHint="@string/hint_buscar"/>  

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Fecha" 
        android:layout_gravity="center_vertical"/> 

       <EditText 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:inputType="none" 
        android:focusable="false" 
        android:id="@+id/etFecha" 
        android:layout_gravity="center_vertical"/> 
      </GridLayout>  

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/btn_ver_horarios" 
       android:id="@+id/buttonIniciarSesion" 
       android:textColor="@color/azul" 
       android:background="?android:attr/selectableItemBackground" 
       android:layout_gravity="center_horizontal" 
       android:onClick="verHorarios" 
       android:paddingLeft="8dp" 
       android:paddingRight="8dp" 
       android:layout_marginTop="15dp"/> 

     </LinearLayout> 
+0

你不需要的网格布局可言。您可以用一个RelativeLayout替换LinearLayout。 –

+0

好的,然后告诉我一个答案,以实现我正在寻找的东西:标签“buscar”必须与搜索视图垂直居中。与编辑文本相关的标签“fecha”也一样。但edittext和searchview必须垂直对齐(从同一个X坐标开始,尽管一个在另一个之上)。 –

回答

0

OK,这里是我ened了,使您的布局只使用单一的RelativeLayout(避免讨厌的嵌套布局是更好的演出)。

棘手的部分是让TextViews与它们的可编辑视图垂直对齐。
通过给所有视图解决相同的固定宽度(40 dp原来是一个很好的价值)。

这是结果:

enter image description here

注:因为我不使用你的主题(也不材料设计 - 因为我是一个自由思想家和喜欢做的事情我的方式,ALWAYS) ,我为父布局使用了背景颜色。

这留下了一个白色的边框(没有时间和意志来解决这个问题)。 你不会有这个问题。只要将它从RelativeLayout中移除即可。


这是我使用的代码(在评论中,你会发现如何恢复你的搜索查看和你的按钮):

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#bdf" 
    android:layout_margin="8dp" 
    > 

    <TextView 
     android:id="@+id/tvRecorrido" 
     android:layout_width="wrap_content" 
     android:layout_height="40dp" 
     android:text="Buscar" 
     android:gravity="center_vertical" 
     android:layout_marginRight="8dp" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
    /> 
<!-- <SearchView --> 
<!--  android:id="@+id/svBusqueda" --> 
<!--  android:layout_width="match_parent" --> 
<!--  android:layout_height="40dp" --> 
<!--  android:queryHint="@string/hint_buscar" --> 
<!--  android:layout_alignParentTop="true" --> 
<!--  android:layout_alignParentRight="true" --> 
<!--  android:layout_toLeftOf="@id/tvRecorrido" --> 
<!-- /> --> 



<!-- 
    For my test project only, I replaced the SerachView (requires API Level 11) 
    with an EditText (I support API Level 8+) 
--> 
<!-- You can simply remove my EditText (below) and uncomment your SearchView (above) --> 

    <EditText 
     android:id="@+id/svBusqueda" 
     android:layout_width="match_parent" 
     android:layout_height="40dp" 
     android:queryHint="This was: @string/hint_buscar" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentRight="true" 
     android:layout_toRightOf="@id/tvRecorrido" 
    /> 

    <TextView 
     android:id="@+id/tvFecha" 
     android:layout_width="wrap_content" 
     android:layout_height="40dp" 
     android:text="Fecha" 
     android:gravity="center_vertical" 
     android:layout_marginRight="8dp" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@id/svBusqueda" 
    /> 
    <EditText 
     android:id="@+id/etFecha" 
     android:layout_width="match_parent" 
     android:layout_height="40dp" 
     android:inputType="none" 
     android:focusable="false" 
     android:layout_gravity="center_vertical" 
     android:layout_alignParentRight="true" 
     android:layout_toRightOf="@id/tvFecha" 
     android:layout_below="@id/svBusqueda" 
    /> 

<!-- <Button --> 
<!--  android:layout_width="wrap_content" --> 
<!--  android:layout_height="wrap_content" --> 
<!--  android:text="@string/btn_ver_horarios" --> 
<!--  android:id="@+id/buttonIniciarSesion" --> 
<!--  android:textColor="@color/azul" --> 
<!--  android:background="?android:attr/selectableItemBackground" --> 
<!--  android:onClick="verHorarios" --> 
<!--  android:paddingLeft="8dp" --> 
<!--  android:paddingRight="8dp" --> 
<!--  android:layout_marginTop="15dp" --> 
<!--  android:layout_centerHorizontal="true" --> 
<!--  android:layout_alignBottom="@id/etFecha" --> 
<!-- /> --> 



<!-- For my test project only, I removed: --> 

<!--  android:textColor="@color/azul" --> 
<!--  android:background="?android:attr/selectableItemBackground" --> 
<!--  android:text="@string/btn_ver_horarios" --> 

<!-- I used fixed values in place of the string and the color resources --> 
<!-- You can simply remove my Button (below) and uncomment yours (above) --> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="VER HORARIOS" 
     android:background="#bdf" 
     android:id="@+id/buttonIniciarSesion" 
     android:textColor="#4af" 
     android:onClick="verHorarios" 
     android:paddingLeft="8dp" 
     android:paddingRight="8dp" 
     android:layout_marginTop="15dp" 
     android:layout_centerHorizontal="true" 
     android:layout_below="@id/etFecha" 
    /> 
</RelativeLayout> 
+0

但如果用户更改设备的文本大小? (来自设备配置)。如果文字看起来大于40dp,他们会包装,我不希望这样。这就是为什么我想使用gridlayout(不明白为什么我不能使用该组件,如果它可用...)。谢谢你的回答:) –

+0

当然你可以。我一直在寻找**优化**(并且避免嵌套布局是一种优化)。如果他的用户做**奇怪的事情**,那不是我的事。他/她知道他们正在搞砸这个系统。 –

+0

当然你可以:)但我正在做一个可能会被老年人使用的应用程序,并且他们有机会在他们的智能手机中使用大文本。这就是为什么我试图预测(除了这是我的carreer tesis的应用程序,并需要处理这些事情)。如果你知道一种方法来做我想要的,使用网格或类似的解决方案,将会对我有很大的帮助。谢谢! –