Android - Центр Textview в LinearLayout
Я пытаюсь центрировать TextView
в LinearLayout
и он центрируется горизонтально, но не вертикально.
ниже мой код
<LinearLayout
android:orientation="vertical"
android:layout_width="320dp"
android:layout_height="50dp"
android:background="@drawable/rectblack"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:text="Explode a Vin"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tvExVin" />
</LinearLayout>
В конце я бы хотел, чтобы он был отцентрирован вертикально слева от Linearlayout
Если бы кто-то мог понять, как это сделать, это было бы здорово.
Спасибо!
3 ответа
У вас гравитация и layout_gravity поменялись местами. Вы можете иметь несколько значений для атрибута гравитации, разделенных "|". Попробуй это:
<LinearLayout
android:orientation="vertical"
android:layout_width="320dp"
android:layout_height="50dp"
android:background="@drawable/rectblack"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical|left"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:text="Explode a Vin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvExVin" />
</LinearLayout>
В целях разъяснения:
android:layout_gravity="center_horizontal"
будет выравнивать LinearLayout горизонтально в его родительском элементе.
android:gravity="center_vertical|left"
центрирует TextView (и любые другие дочерние элементы) вертикально внутри LinearLayout и выравнивает его по левому краю.
Попробуйте изменить свой gravity
в
android:layout_gravity="center_vertical"
gravity
используется для позиционирования контента внутри вашего View
а также layout_gravity
используется для позиционирования View
в пределах своего родителя.
Но меня смущает "В конце я бы хотел, чтобы он был отцентрирован вертикально слева от линейного разметки". Но, судя по всему, мой ответ должен дать вам то, что вы хотите.
Кроме того, если нет очень веских причин, вам не нужно использовать LinearLayout
или любой родитель, для одного ребенка. Если вы можете дать лучшее объяснение того, что вы хотите, или даже снимок экрана, мы сможем помочь с лучшим решением.
Это даст вам желаемый эффект
<LinearLayout
android:layout_width="320dp" <!-- removed orientation (horizontal by default) -->
android:layout_height="50dp"
android:background="@drawable/rectblack"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:text="Explode a Vin"
android:layout_gravity="center_vertical" <!-- changed gravity here -->
android:layout_width="fill_parent"
android:layout_height="wrap_content" <!-- change to wrap_content -->
android:id="@+id/tvExVin" />
</LinearLayout>
<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:layout_gravity="center_horizontal|center_vertical"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AppLock Demo"
android:textSize="30dp" />
</LinearLayout>