如何在Android中建立一個整齊的兩列輸入表單?
我想建立一個整齊的兩列輸入表單,如下所示:
我的xml佈局程式碼到目前為止
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.25" android:text="@string/lblTitle" /> <EditText android:id="@+id/txtTitle" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.75" android:ems="10" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.25" android:text="@string/lblAuthor" /> <EditText android:id="@+id/txtAuthor" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.75" android:ems="10" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.25" android:text="@string/lblPublisher" /> <EditText android:id="@+id/txtPublisher" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.75" android:ems="10" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.25" android:text="@string/lblIsbn" /> <EditText android:id="@+id/txtIsbn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.75" android:ems="10" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <View android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.25" /> <Button android:id="@+id/btnSubmit" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.375" android:text="@string/submit" /> <Button android:id="@+id/btnCancel" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.375" android:text="@string/cancel" /> </LinearLayout> </LinearLayout>
你可以看到我使用了LinearLayout,利用layout_weight& layout_width =“0dp”,使兩列分割槽看起來整潔.在HTML程式碼中,我們可以使用%width size.但是在android xml佈局中,layout_width沒有%-value.我想避免列寬度的硬編碼dp值.是否可能在Android?
到目前為止,這是我能做的最好的.現在我想將ISBN文字框的大小變小(從當前大小減小50%).但是,由於layout_width必須為“0dp”才能使layout_weight工作,因此無法調整文字框寬度.我也想使用提交和取消按鈕大小做同樣的事情.
我不知道是否使用LinearLayout是一個正確的方法來建立輸入形式整齊地在android中. TableLayout可以更好嗎?我聽說你不能在TableLayout中更改子檢視的layout_width.
這裡有一些人推薦我使用RelativeLayout,我嘗試過但不支援layout_weight.
好的,我發現了LinearLayout的方式.訣竅是通過使用LinearLayout包裝要調整大小的檢視.那麼View本身的寬度可以通過使用:android:layout_width或android:ems屬性來調整.
這是xml程式碼:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.25" android:text="@string/lblTitle" /> <EditText android:id="@+id/txtTitle" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.75" android:ems="10" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.25" android:text="@string/lblAuthor" /> <EditText android:id="@+id/txtAuthor" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.75" android:ems="10" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.25" android:text="@string/lblPublisher" /> <EditText android:id="@+id/txtPublisher" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.75" android:ems="10" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="0.25" android:text="@string/lblIsbn" /> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.75" android:orientation="horizontal" > <EditText android:id="@+id/txtIsbn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="5" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <View android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.25" /> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.75" android:orientation="horizontal" > <Button android:id="@+id/btnSubmit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="5" android:text="@string/submit" /> <Button android:id="@+id/btnCancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="5" android:text="@string/cancel" /> </LinearLayout> </LinearLayout> </LinearLayout>
程式碼日誌版權宣告:
翻譯自:http://stackoverflow.com/questions/10751094/how-to-create-a-neat-two-columns-input-form-in-android