次のようなきちんとした 2 列の入力フォームを作成したいと思います。
これまでの私の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>
layout_weight & layout_width="0dp" トリックを利用した LinearLayout を使用して、2 つの列の分割がきれいに見えるようにしたことがわかります。HTML コードでは、% width size で使用できます。しかし、android xml レイアウトでは、layout_width の % 値はありません。列幅のハードコーディングされた dp 値を避けたい。アンドロイドで可能ですか?
これまでのところ、それが私にできる最善のことです。ここで、ISBN テキストボックスのサイズを変更して小さくしたいと考えています (現在のサイズの 50% 小さいサイズ)。ただし、layout_weight が機能するには、layout_width を「0dp」にする必要があるため、テキストボックスの幅を変更できません。送信ボタンとキャンセルボタンのサイズについても同じことをしたいと思います。
Androidできちんと入力フォームを作成するには、LinearLayoutを使用するのが正しい方法なのだろうか。これには TableLayout の方が適しているでしょうか? TableLayout で子ビューの layout_width を変更できないと聞きました。
ここで RelativeLayout の使用を勧める人がいます。試してみましたが、layout_weight をサポートしていません。