0

XML :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/fragone"
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout_weight="8"
              android:background="#4caad4"
              android:orientation="vertical">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="4"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/image1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center"/>
    </LinearLayout>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="62dp"/>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:textAlignment="center"
            android:textColor="#ffffff"
            android:textSize="18sp"
            android:textStyle="bold"/>


        <TextView
            android:id="@+id/tv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:textAlignment="center"
            android:textColor="#ffffff"
            android:textSize="13sp"
            />

    </LinearLayout>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="50dp"/>
</Linearlayout>

PREVIEW :enter image description here

It works perfectly, but I want to align text view. How do they do it? In this size perfect for horizontal view. In portrait view it align looks like this picture. How to set both horizontal & portrait view on same?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • You can use this link: [Gravity and layout_gravity on Android](http://stackoverflow.com/questions/3482742/gravity-and-layout-gravity-on-android) – Pouria Ghavidel Dec 08 '16 at 08:15

2 Answers2

0

where do you want to align your textview, to right, left or center or any predefined position

I am exactly not clear about your ques, please specify it and tell what you want exactly.

Here I have fixed some lines of your layout, as you are not very specific to your question

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4caad4"
android:orientation="vertical"
android:weightSum="8">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="4"
    android:gravity="center"
    android:weightSum="4"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/image1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:gravity="center"
        android:textColor="#fff"
        android:text="text"
        android:textSize="20sp"
        android:layout_weight="2"/>
</LinearLayout>

<TextView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:text="text"
    android:gravity="center"
    android:textColor="#fff"
    android:layout_weight="1" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2"
    android:orientation="vertical"
    android:weightSum="2">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:gravity="center"
        android:text="text"
        android:textAlignment="center"
        android:textColor="#ffffff"
        android:textSize="18sp"
        android:textStyle="bold" />


    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:gravity="center"
        android:text="text"
        android:textAlignment="center"
        android:textColor="#ffffff"
        android:textSize="13sp" />

</LinearLayout>

<TextView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:text="text"
    android:gravity="center"
    android:textColor="#fff"
    android:layout_weight="1" />
</LinearLayout>
Ranjan
  • 1,326
  • 18
  • 38
  • i want to add textview below the image –  Dec 08 '16 at 08:18
  • Here,I have edited the ans for you, change the weight if you want a bigger or smaller textView or imageView – Ranjan Dec 08 '16 at 08:28
  • Let me know if this solves your problem, and don't forget to upvote – Ranjan Dec 08 '16 at 08:29
  • i want to align textview one by one in vertical view –  Dec 08 '16 at 09:08
  • use layout weight to align, suppose in your case your linear layout contain "weight sum"=8 means whole screen is divided into 8 section. – Ranjan Dec 08 '16 at 09:39
  • if orientation is vertical – Ranjan Dec 08 '16 at 09:41
  • set height to 0dp and layout_weight as you want – Ranjan Dec 08 '16 at 09:41
  • vise versa for horizontal layout, – Ranjan Dec 08 '16 at 09:42
  • yes.i get this.but i have another problem pop up.it is when i run small size emulator in horizontal view second textview not fully show.how to show it? –  Dec 08 '16 at 09:54
  • the xml is correct, weather you open in horizontal or vertical mode. for better understanding i have putted a textview in it which is centered. I checked it in 4 devices. – Ranjan Dec 08 '16 at 10:32
  • maybe you are entering more than one lines in textview, which may occupy the space of other textview too. – Ranjan Dec 08 '16 at 10:33
  • its because you 2nd last textview contains more than one line – Ranjan Dec 08 '16 at 10:35
  • it should be not more than the weight height, and according to your ques. the above xml is working fine, and below is the image of it. – Ranjan Dec 08 '16 at 10:51
0

Horizontal view

Vertical view

it is working fine in both mode.

Ranjan
  • 1,326
  • 18
  • 38
  • what is difference weightsum & layout_weight? –  Dec 08 '16 at 11:26
  • weightsum defines how much weight you have with you and layout_weight is how much you are giving from weightsum. – Ranjan Dec 08 '16 at 11:31
  • I am also a beginner dude, perhaps you can give a up vote, if my ans is right. – Ranjan Dec 08 '16 at 11:51
  • how to vote? & if any further doubts how i contact you? –  Dec 08 '16 at 12:00
  • Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score. i have this message –  Dec 08 '16 at 13:09