0

I need to create this :

enter image description here

As you can see the lines not exactly centered with the text .

The problem ever bigger with Lower case text .

the text is to low , not centered in is own view

this is my code - The container is Linear Layout with

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:gravity="center"
android:orientation="horizontal

<View
    android:layout_width="0dp"
    android:layout_height="1dp"
    android:layout_marginRight="10dp"
    android:layout_weight="1"
    android:background="@color/gray_separator" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:text="How ?"
    android:textColor="@color/gray_login_or"
    android:textSize="26sp" />

<View
    android:layout_width="0dp"
    android:layout_height="1dp"
    android:layout_marginLeft="10dp"
    android:layout_weight="1"
    android:background="@color/gray_separator" />

i have tried with Relative Layout but this is not the problem , the problem and the solution in the TextView .

The best easy way to understand the problem is the different between lower and upper case...in upper case i have no problem .

Phantômaxx
  • 36,442
  • 21
  • 78
  • 108
Jesus Dimrix
  • 4,025
  • 4
  • 24
  • 60

6 Answers6

1

I have used :

 android:layout_gravity="center_horizontal"

instead of :

  android:gravity="center_vertical"

And it worked for me.

Rozina
  • 349
  • 2
  • 14
1
<View
    android:layout_width="0dp"
    android:layout_height="1dp"
    android:layout_weight="1"
    android:layout_gravity="center"
    android:background="@color/gray_separator"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:textSize="26sp"
    android:text="How ?"
    android:textColor="@color/gray_login_or"/>

<View
    android:layout_width="0dp"
    android:layout_height="1dp"
    android:layout_weight="1"
    android:layout_gravity="center"
    android:background="@color/gray_separator"/>
ashazar
  • 714
  • 5
  • 10
1

Here's what I have done for the similar requirement, which works pretty well.

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="4"
        >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="5dp"
            android:src="@drawable/dashline"
            android:layerType="software"
            android:layout_below="@+id/title"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="0dp"
            android:layout_marginBottom="0dp"
            android:layout_weight="2"
            android:layout_gravity="center"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="OR"
            android:textAllCaps="false"
            android:textColor="@color/white"
            android:textSize="25dp"
            android:layout_margin="10dip"
            android:layout_gravity="center"
            android:clickable="true"
            />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="5dp"
            android:src="@drawable/dashline"
            android:layerType="software"
            android:layout_below="@+id/title"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="0dp"
            android:layout_marginBottom="0dp"
            android:layout_gravity="center"
            android:layout_weight="2"
            />

    </LinearLayout>

I am using ImageView as per my need, you can replace them with View which you want.

This is my dashline.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">

    <stroke
        android:color="#ffffff"
        android:dashWidth="8px"
        android:dashGap="0px"
        android:width="1dp"/>
</shape>

Here's the snapshot of the screen portion.

enter image description here

So android:layout_gravity="center" should work.

Ana
  • 116
  • 14
1

use android:gravity="center" in your text view.

Jawad Malik
  • 615
  • 3
  • 20
1

I guess parent is a horizontal linear layout. So this should be used:

android:layout_gravity="center_vertical"

instead of

android:gravity="center_vertical"

but if parent's height is wrap_content, result should be same.

BTW, alphabet fonts are not horizontally in the middle of the space. It is a little lower than center line, which is more comfortable for human eyes. refer to:

https://en.wikipedia.org/wiki/File:Typography_Line_Terms.svg

AIMIN PAN
  • 419
  • 3
  • 10
1
                <android.support.v7.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp">


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:layout_marginTop="0dp"
                    android:orientation="horizontal">

                    <View
                        android:layout_width="40dp"
                        android:layout_height="1dp"
                        android:background="@color/black"
                        android:layout_gravity="center_vertical"
                        android:layout_weight="1"/>

                    <TextView
                        android:layout_gravity="center"
                        android:gravity="center"
                        android:id="@+id/Imagetxt"
                        android:layout_width="20dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="Image Cart"
                        android:clickable="false"
                        android:textColor="@color/orange"/>

                    <View
                        android:layout_width="40dp"
                        android:layout_height="1dp"
                        android:background="@color/black"
                        android:layout_gravity="center_vertical"
                        android:layout_weight="1"/>

            </LinearLayout>


                </android.support.v7.widget.CardView>
Manish Ahire
  • 155
  • 1
  • 16