Horizontal center aligns two text images in layout

I need to horizontally align two textviewin the center of the screen. Both textviewshave different font sizes.

enter image description here

Here is my code:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="top|center_horizontal"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/progressstatus"
        android:layout_gravity="center_horizontal"
        android:textColor="#FFFFFF"
        android:textSize="50sp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/percent"
        android:textColor="#CCCCCC"
        android:textSize="20sp" />

</LinearLayout>

Currently, my text images are aligned to the left, and both show the same font size.

+4
source share
8 answers

Try:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/progressstatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_vertical|right"
        android:text="75"
        android:textColor="#FFFF00"
        android:textSize="50sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:text="%"
        android:textColor="#CCCCCC"
        android:textSize="20sp" />
</LinearLayout>

: wrap_content, , . 2 . , - . , . , wrap_content. - match_parent, , , . , center_aligned.

enter image description here

+10
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#31BBF9" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="top|center_horizontal"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/progressstatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="75"
        android:textColor="#FFFFFF"
        android:textSize="50sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="%"
        android:textColor="#FFFFFF"
        android:gravity="center_vertical"
        android:textSize="20sp" />

</LinearLayout>

enter image description here

testview (%) android:gravity="center_vertical"

+4

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal" >

<TextView
    android:id="@+id/progressstatus"
    android:textColor="#FFFFFF"
    android:textSize="50sp"
    android:gravity="center_vertical"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"/>

<TextView
    android:gravity="center_vertical"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:text="@string/percent"
    android:textColor="#CCCCCC"
    android:textSize="20sp" />

</LinearLayout>
0

<?xml version="1.0" encoding="utf-8"?>
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="top|center_horizontal"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/progressstatus"
            android:textColor="#FFFFFF"
            android:textSize="50sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
    android:text="@string/percent"
            android:textColor="#FFFFFF"
            android:textSize="20sp" />

    </LinearLayout>
0

You can use the Relative layout to accomplish the above. Please check the code below.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

 <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:gravity="center_horizontal"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/progressstatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="75"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/percent"
        android:textSize="20sp" />
  </LinearLayout>

</RelativeLayout>
0
source

Use this code in an XML file

  <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white_color" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/progressstatus"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:textColor="#FFFFFF"
                android:textSize="50sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/percent"
                android:textColor="#CCCCCC"
                android:textSize="20sp" />
        </LinearLayout>
    </RelativeLayout>
0
source

check it out

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">                 

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="20dp" >

        <TextView
            android:id="@+id/progressstatus"
            android:layout_gravity="center_horizontal"
            android:textColor="#FFFFFF"
            android:textSize="50sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/percent"
            android:textColor="#CCCCCC"
            android:gravity="center"
            android:textSize="20sp" />

    </LinearLayout>

 </LinearLayout>
0
source

you can do this using a single text view

            <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/black"
                tools:context="com.example.demo.MainActivity$PlaceholderFragment" >

                <TextView android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/tv"
                    android:text="75%"
                    android:textSize="20sp"
                    android:gravity="top"
                    android:layout_centerHorizontal="true"/>
              </RelativeLayout>


        TextView textView = (TextView) findViewById(R.id.tv);
                Spannable span = new SpannableString(textView.getText());
                span = new SpannableString(textView.getText());
                span.setSpan(new CustomCharacterSpan(), 2, 3,
                        SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);//u can use ur own ratio using another constructor
                textView.setText(span, TextView.BufferType.SPANNABLE);
                span.setSpan(new RelativeSizeSpan(3f), 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                textView.setText(span);



    package com.example.demo;

    import android.text.TextPaint;
    import android.text.style.MetricAffectingSpan;

    public class CustomCharacterSpan extends MetricAffectingSpan {
    double ratio = 1.5;

    public CustomCharacterSpan() {
    }

    public CustomCharacterSpan(double ratio) {
        this.ratio = ratio;
    }

    @Override
    public void updateDrawState(TextPaint paint) {
        paint.baselineShift += (int) (paint.ascent() * ratio);
    }

    @Override
    public void updateMeasureState(TextPaint paint) {
        paint.baselineShift += (int) (paint.ascent() * ratio);
    }}

for additional reference you can check this link Two different styles in one text mode with different severity and hieght

0
source

Source: https://habr.com/ru/post/1542454/


All Articles