Reputation: 6676
I have a LinearLayout and I want to add 3 TextViews vertically. That I can do.
My question is: how do get the TextViews to evenly space themselves out vertically within my LinearLayout which happens to be 800x600 pixels.
Given that the LinearLayout is 600 pixels high, that would mean positioning the 3 lines of text on lines y=150, y=300, y=450 (approximately).
How would I create an XML file to do that even vertical spacing?
Note: I don't want to use absolute coordinates.
Upvotes: 0
Views: 2412
Reputation: 44178
You can use the weight attribute to make the TextView's scale to the size of the window. Furthermore you can specify the gravity attribute to make the text inside of the TextView
's be centered.
In this code, the TextView
's each take up 1/3 of the screen:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv1"
android:layout_weight="1"
android:layout_margin="5dp"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:gravity="center"
android:text="tv1"/>
<TextView
android:id="@+id/tv2"
android:layout_weight="1"
android:layout_margin="5dp"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:gravity="center"
android:text="tv1"/>
<TextView
android:id="@+id/tv3"
android:layout_weight="1"
android:layout_margin="5dp"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:gravity="center"
android:text="tv1"/>
</LinearLayout>
Upvotes: 1
Reputation: 42854
You can do with the help of layout_weight
Sample
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="text1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="text2"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="text3"
android:textAppearance="?android:attr/textAppearanceLarge" />
Upvotes: 1
Reputation: 3791
below is the code to evenly manage your text view just copy paste it
when you use weight and gravity in your xml code for designing it will automatically adjust on all screen
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#ffffff"
android:text="1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#ffffff"
android:text="2"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#ffffff"
android:text="3"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" />
</LinearLayout>
Upvotes: 2