2016-01-13 22 views
0

我想要GridLayout在Android Studio 1.5中創建一個佈局。最初的XML文件如下:如何理解Android中的GridLayout?

<?xml version="1.0" encoding="utf-8"?> 
<GridLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:useDefaultMargins="true" 
    android:alignmentMode="alignBounds" 
    android:columnOrderPreserved="false" 
    android:columnCount="10" 
    android:background="#cccccc" 
    > 

    <TextView 
     android:text="Title" 
     android:textSize="32dip" 
     android:layout_columnSpan="10" 
     android:layout_gravity="center_horizontal" 
     android:id="@+id/textView1" 
     android:layout_row="0" 
     android:layout_column="0" /> 


    <TextView 
     android:text="@string/new_name" 
     android:layout_gravity="right" 
     android:layout_row="2" 
     android:layout_column="0" 
     /> 

    <EditText 
     android:ems="6" 
     android:singleLine="true" 
     android:inputType="textCapWords" 
     android:id="@+id/new_name" 
     android:layout_row="2" 
     android:layout_column="1" /> 

    <ImageButton 
     android:src="@mipmap/ic_backspace_black_24dp" 
     android:id="@+id/clear_line" 
     android:layout_row="2" 
     android:layout_column="8" 
     /> 

    <Button 
     android:text="Search" 
     android:id="@+id/new_search" 
     android:background="@color/colorPrimaryDark" 
     android:layout_column="9" 
     android:layout_row="2" /> 


    <TextView 
     android:text="@string/new_label" 
     android:layout_gravity="right" 
     android:layout_row="3" 
     android:layout_column="0" /> 



    <EditText 
     android:ems="6" 
     android:singleLine="true" 
     android:inputType="textCapWords" 
     android:id="@+id/new_label" 
     android:layout_row="3" 
     android:layout_column="1" /> 

    ______ 

    <TextView 
     android:layout_column="0" 
     android:text="Row3" 
     android:layout_gravity="right" 
     /> 

    <EditText 
     android:ems="7" 
     android:singleLine="true" 
     android:id="@+id/Row3" 
     android:inputType="numberDecimal" 
     android:layout_row="4" 
     android:layout_column="1" /> 

    <TextView 
     android:layout_column="0" 
     android:text="Row4" 
     android:layout_gravity="right" 
     android:layout_row="5" /> 

    <EditText 
     android:ems="7" 
     android:singleLine="true" 
     android:id="@+id/Row5" 
     android:inputType="number" 
     android:layout_row="5" 
     android:layout_column="1" /> 



    <Space 
     android:layout_row="4" 
     android:layout_column="0" 
     android:layout_columnSpan="3" 
     android:layout_gravity="fill" 
     /> 

    <Button 
     android:text="Cancel" 
     android:id="@+id/new_cancel" 
     android:background="@color/colorPrimaryDark" 
     android:layout_row="10" 
     android:layout_column="0" 
     /> 
    <Button 
     android:text="Ok" 
     android:id="@+id/new_ok" 
     android:background="@color/colorPrimaryLight" 
     android:layout_row="10" 
     android:layout_column="9" 
     /> 
</GridLayout> 

其中,在預覽,呈現類似如下:

enter image description here

  1. 然而,我的手機上我看到,退格重疊有所用「名稱」的文本輸入框。這是我的手機還是Android Studio的問題?

  2. 我改變佈局的第一個輸入框,如下所示:

<EditText 
    android:singleLine="true" 
    android:inputType="textCapWords" 
    android:id="@+id/new_name" 
    android:columnCount="4" 
    android:layout_row="2" 
    android:layout_column="1" 
/> 

這使得實際的輸入框很小,雖然我已經指定columnCount="4"。這個屬性似乎沒有在這裏任何影響:

enter image description here

  • 我恢復這一變化,並嘗試進行如下調整退格圖像的大小:
  • <ImageButton 
        android:src="@mipmap/ic_backspace_black_24dp" 
        android:id="@+id/clear_line" 
        android:layout_row="2" 
        android:layout_column="8" 
        android:minHeight=4 
        android:minWidth=4 
        android:maxHeight=4 
        android:maxWidth=4 
        /> 
    

    但它不會更改退格圖像的大小。我還可以設置成數字paranthesis,這會觸發一個錯誤:

    `Cannot resolve symbol '4'` 
    

    每隔數可以把括號,但不是在這裏?

    這有什麼邏輯嗎?還是人們在創建android框架時關閉了邏輯?有人能解釋我與那些沒有味道的東西嗎?以及如何更改退格圖像的大小?我查看了ImageButton doc,但找不到任何邏輯屬性,如「大小」...!

    回答

    1

    在你的情況,我看不出一個理由使用GridLayout,反正這裏的問題的答案:

    1# - 在你的手機,你會發現退格與重疊'名稱'EditText,因爲沒有足夠的寬度來繪製一個旁邊的4個視圖。


    2# - 沒有必要爲android:columnCount屬性:

    The maxmimum number of columns to create when automatically positioning children.

    這就是爲什麼我們什麼也沒做。


    3# - 當你嘗試這種代碼:

    <ImageButton 
        android:src="@mipmap/ic_backspace_black_24dp" 
        android:id="@+id/clear_line" 
        android:layout_row="2" 
        android:layout_column="8" 
        android:minHeight=4 
        android:minWidth=4 
        android:maxHeight=4 
        android:maxWidth=4/> 
    

    你得到Cannot resolve symbol '4'錯誤,因爲:

    • 1)4必須有兩個"之間。
    • 2)您必須指定單位(dp,px,sp ...)。

    這是應該的:

    <ImageButton 
         android:src="@mipmap/ic_backspace_black_24dp" 
         android:id="@+id/clear_line" 
         android:layout_row="2" 
         android:layout_column="8" 
         android:minHeight="15dp" 
         android:minWidth="15dp" 
         android:maxHeight="15dp" 
         android:maxWidth="15dp"/> <!-- i used 15dp instead of 4dp because 4dp is too small --> 
    

    4# - 要改變的View大小(Button你的情況),您可以使用android:layout_widthandroid:layout_height屬性:

    <ImageButton 
        android:layout_width="12dp" 
        android:layout_height="12dp" 
        android:src="@mipmap/ic_backspace_black_24dp" 
        android:id="@+id/clear_line" 
        android:layout_row="2" 
        android:layout_column="8"/>