2015-10-30 80 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的應用程序,並需要處理這些事情)。如果你知道一種方法來做我想要的,使用網格或類似的解決方案,將會對我有很大的幫助。謝謝! –