1

複数の水平線形レイアウトがあります。縦にリストされているこれらの各レイアウトの中に、TextView とそれに続く EditText があります。すべての EditText ビューに一定の左マージンを与えて、それらが整列するようにしたいのですが、どうすればそれを達成できますか? 部分的なコードは次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="5dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/pNameL"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="Segoe ui"
            android:text="Name"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText2"
            style="@style/editText_style"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10" >
        </EditText>

    </LinearLayout>
4

1 に答える 1

2

res/values/style.xml ファイルにスタイルを作成します。style.xml が存在しない場合は、新しいものを作成します。

<style name="editText_style">
    <item name="android:layout_marginLeft">30dp</item>
    <item name="android:layout_marginRight">30dp</item>
</style>

次に、(余白を変更したい) レイアウト ファイルの<EditText>タグの下で、次のスタイルを使用して適用します。

style="@style/editText_style"


編集:親が水平方向の場合、親に対してマージンを適用することはできません。プロパティ

を使用してみてください。layout:weightたとえば、EditText を親の半分から開始し、EditText を含む親に合計 2 つのビューがある場合layout:weight、両方のビューを 0.5 に設定します。これにより、両方のビューの親に等スペースが与えられます。

これはLinearLayouts、変更したいすべての に等しい no が含まれている場合にのみ機能します。ビューの。異なる場合は、LinearLayout異なる番号が含まれています。ビューの場合、スタイルを使用できないか、単一の場所から変更する方法がありません。

注:使用layout:weightプロパティでは、静的マージン値を取得できません。親のサイズに依存し、画面サイズによって異なる場合があります。

于 2013-06-15T17:06:55.717 に答える