Центрировать Editext в линейной компоновке - Android - PullRequest
0 голосов
/ 12 февраля 2020

Я пытаюсь центрировать текст редактирования, который вы видите на этом экране (тот, что на черных кнопках): enter image description here

Это мой код. Я установил тег android: gravity = "center_horizontal" , но, как вы можете видеть, он не работает. Я не понимаю, нужно ли мне установить его в центре линейного расположения или нужно обратить внимание на внешнее линейное расположение:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FCFCFC"
    android:orientation="vertical"
    android:layout_marginTop="8dp"
    tools:context=".PaintingActivity" >

    <!-- Top Buttons -->

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:orientation="horizontal" >


        <ImageButton
            android:id="@+id/draw_btn"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_marginRight="50dp"
            android:contentDescription="@string/brush"
            android:background="@drawable/rounded_corners"
            android:padding="16dp"
            android:layout_marginTop="5dp"
            android:src="@drawable/brush" />

        <ImageButton
            android:id="@+id/erase_btn"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_marginRight="50dp"
            android:contentDescription="@string/erase"
            android:background="@drawable/rounded_corners"
            android:padding="16dp"
            android:layout_marginTop="5dp"
            android:src="@drawable/eraser" />

        <ImageButton
            android:id="@+id/undo_btn"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:contentDescription="Undo"
            android:layout_marginRight="50dp"
            android:background="@drawable/rounded_corners"
            android:padding="16dp"
            android:layout_marginTop="5dp"
            android:src="@drawable/undo" />

        <ImageButton
            android:id="@+id/colorPicker"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_marginTop="5dp"
            android:background="@drawable/rounded_corners"
            android:contentDescription="colorPicker"
            android:padding="16dp"
            android:src="@drawable/palette" />

    </LinearLayout>

    <!-- Custom View -->

    <com.example.williamstest.DrawingView
        android:id="@+id/drawing"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="3dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="3dp"
        android:layout_weight="1"
        android:background="#FFFFFFFF" />

    <!-- Menu in basso -->

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="vertical" >

        <!-- Top Row -->

        <LinearLayout
            android:id="@+id/toprow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/_70sdp"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/edittitle"
                android:layout_width="400dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dip"
                android:layout_marginBottom="25dip"
                android:ems="10"
                android:hint="Inserisci qui il titolo"
                android:gravity="center_horizontal"
                android:inputType="textNoSuggestions"
                android:singleLine="true"/>

        </LinearLayout>

        <!-- Bottom Row -->

        <LinearLayout
            android:id="@+id/bottomrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="120dp"
            android:orientation="horizontal">

            <Button
                android:id="@+id/bb_3"
                android:layout_width="100dp"
                android:layout_height="50dp"
                android:layout_marginLeft="120dp"
                android:layout_marginBottom="10dp"
                android:background="@drawable/button_bg"
                android:text="Indietro"
                android:textColor="#FFFFFF" />

            <Button
                android:id="@+id/bb_2"
                android:layout_width="100dp"
                android:layout_height="50dp"
                android:layout_marginLeft="120dp"
                android:layout_marginBottom="10dp"
                android:layout_weight="1"
                android:background="@drawable/button_bg"
                android:text="Avanti"
                android:textColor="#FFFFFF" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

Ответы [ 2 ]

1 голос
/ 12 февраля 2020

У вас избыточное левое поле Удалите android:layout_marginLeft="@dimen/_70sdp" из макета toprow или добавьте равное правое поле

0 голосов
/ 12 февраля 2020

Нет необходимости использовать layout_marginLeft для выравнивания center_horizontal, удалите его. Просто добавьте android:gravity к center_horizontal в вашем LinearLayout корпусе EditText.

<LinearLayout
    android:id="@+id/toprow"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/edittitle"
        android:layout_width="400dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dip"
        android:layout_marginBottom="25dip"
        android:ems="10"
        android:hint="Inserisci qui il titolo"
        android:gravity="center_horizontal"
        android:inputType="textNoSuggestions"
        android:singleLine="true"/>

</LinearLayout>
...