Reputation: 675
I have 3 TextViews inside a vertical LinearLayout as below :
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/green"
tools:context=".SplashActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:gravity="center"
android:text="FirstText"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:gravity="center"
android:text="Second"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/lightGray"
android:gravity="center"
android:text="ThirdText"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Current output of the above code :
But I want the texts to be align left with each other like this :
How do I achieve this?
Upvotes: 0
Views: 34
Reputation: 54244
You need to have a top-level parent that centers its content. Inside that, you'll have a mid-level parent that left-justifies its content. And inside that you'll have your three TextViews.
The key to making this work is to have the mid-level parent only be as wide as its contents. That way, the widest child will define the overall size of the mid-level parent, and then the top-level parent can center the group of them.
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FirstText"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ThirdText"/>
</LinearLayout>
</FrameLayout>
Upvotes: 2
Reputation: 87
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimary"
android:text="FirstText"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimary"
android:text="Second"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimary"
android:text="ThirdText"/>
</LinearLayout>
Upvotes: -1