6

線形レイアウトで edittext を垂直方向と水平方向の中央に配置しようとしていますが、うまくいきません。非常に単純な問題です。

 <?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="fill_parent"
 android:orientation="vertical" 
 android:background="@drawable/login">



<EditText
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:background="@drawable/loginbutton"
    android:text="Username"
    android:layout_gravity="center"
    android:textSize="25dp"
    android:gravity="center_horizontal|center_vertical"
    android:textColor="#000000"
    android:layout_margin="10dp"
    android:id="@+id/username">
  </EditText>

とりあえずrelativelayoutに変更してみます。

4

4 に答える 4

12

LinearLayout私の知る限り、アイテムを積み重ねる方向のセンタリングはサポートしていません。が重要でない限りLinearLayout(あなたの場合、センタリングも必要なのでそうすべきではありません)、 に切り替えることをお勧めしますRelativeLayout
を使用するRelativeLayoutと、 の属性android:layout_centerInParent="true"を設定しEditTextて中央に配置できます。

于 2012-01-24T15:53:08.947 に答える
1

重力を使用すると、UI アイテムのコンテンツの重力に影響します。これは、(この場合) EditText 内のテキストが中央に配置されることを意味します。UI アイテム自体を中央に配置するには、layout_gravity を定義する必要があります。これは、UI アイテムが親内で保持する重力であるためです。OPによって投稿されたコードは、水平方向の中央を行いますが、垂直方向の中央は行いません。

代わりに、RelativeLayout を使用できます

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/login"
android:orientation="vertical" >

<EditText
    android:id="@+id/username"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:background="@drawable/loginbutton"
    android:layout_centerInParent="true"
    android:text="Username"
    android:textColor="#000000"
    android:textSize="25dp" >
</EditText>

</RelativeLayout>
于 2012-01-24T15:39:22.670 に答える
-1

これを試して:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" 
 android:background="@drawable/login"
 android:layout_centerVertical="true"
 android:layout_centerInParent="true">
于 2012-01-24T15:40:08.817 に答える