I am using a LinearLayout that has 2 TextViews. At the moment the first TextView should be on the top and in the center, whereas it is actually displayed on the left?
The second TextView should be on the bottom and in the center, whereas it is being displayed on the right?
I have been trying using layout_gravity and gravity but I just can’t get them to be shown correctly? Any help appreciated.
activity_main.xml
<LinearLayout
android:id="@ id/linearLayout"
android:layout_width="100dp"
android:layout_height="43dp"
android:layout_alignParentBottom="true"
android:layout_marginStart="10dp"
android:layout_marginTop="116dp"
android:layout_marginBottom="37dp"
android:background="@drawable/frame_border"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/textView"
app:layout_constraintVertical_bias="0.72">
<TextView
android:id="@ id/textSpaceRowsVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center"
android:text="TOP 1"
android:textAllCaps="true"
android:textColor="#000"
tools:layout_editor_absoluteX="75dp"
tools:layout_editor_absoluteY="608dp"
tools:ignore="RtlHardcoded" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="center"
android:text="Bottom 2"
android:textColor="#000"
tools:layout_editor_absoluteX="60dp"
tools:layout_editor_absoluteY="584dp" />
</LinearLayout>
CodePudding user response:
Your orientation is lining elements horizontally, where it looks like you want vertically. In your LinearList change the orientation from this:
android:orientation="horizontal"
to this:
android:orientation="vertical"
Then update the gravity of the TextView, so its centered. Change this in the TextViews:
android:layout_gravity="top|center"
to:
android:layout_gravity="center"
CodePudding user response:
use this code on LinearLayout
android:orientation="vertical"
<TextView
android:id="@ id/textSpaceRowsVal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="TOP 1"
android:textAllCaps="true"
android:textColor="#000"
tools:ignore="RtlHardcoded"
tools:layout_editor_absoluteX="75dp"
tools:layout_editor_absoluteY="608dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="center"
android:text="Bottom 2"
android:textColor="#000"
tools:layout_editor_absoluteX="60dp"
tools:layout_editor_absoluteY="584dp" />