Center text relative to layout

I want to center the text inside TextViewrelative to the parent layout, and not relative to the text view.

An example in this code is to select text list_titlerelative to the width of the screen:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
        android:layout_weight="75"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list_title" />
    <Button
        android:layout_weight="15"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="90px"
        android:text="@string/filter"
        android:visibility="gone"
        android:id="@+id/button_filter" />
</LinearLayout>

Do you have a solution?

thanks for the help

+3
source share
5 answers

Hmm, this is not a good solution, but if you know your name length, you can add

android:paddingLeft="160dip"

to your text.

Replace 160dip with 160 - the length of your name is / 2, it will be almost in the center on all screens ...

Edit: or if you want a different solution, you should use a relative layout.

0
source

I would go with the center of the TextView and set its width to wrap_content:

<TextView
    android:layout_weight="75"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/list_title"
    android:layout_centerHorizontal="true" />
+4

, TextView / ?

Make your parent RelativeLayout layout and set the TextView "Center in Parent" true.

+2
source

Try this in xml:

android:gravity="center"
+1
source

Try something like this -

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


                <RelativeLayout android:id="@+id/widget21"
                    android:orientation="horizontal" android:background="@drawable/header"
                    android:layout_height="wrap_content" android:layout_width="wrap_content"
                    android:layout_below="@id/widget20" android:layout_gravity="bottom"


                    >
                        <RelativeLayout android:id="@+id/widget21"
                        android:layout_marginTop="37dip"
                    android:orientation="horizontal" 
                    android:layout_height="wrap_content" android:layout_width="fill_parent"
                    android:layout_below="@id/widget20">

                    <TextView android:id="@+id/section_header_text" 

                        android:layout_width="wrap_content" android:textSize="24dip"
                        android:textStyle="bold"
                        android:text="@string/section_header_text" android:layout_height="wrap_content"
                        android:textColor="#ffffff" android:gravity="bottom"

                        android:layout_centerInParent="true"



                        ></TextView>
                        </RelativeLayout>

                </RelativeLayout>

                <ListView android:id="@+id/android:list" android:layout_width="fill_parent"
                    android:layout_height="wrap_content" android:scaleType="fitEnd" />
                <TextView android:id="@android:id/empty" android:layout_width="fill_parent"
                    android:layout_height="wrap_content" android:text="@string/retrieving_text"
                    android:gravity="center_vertical" android:layout_marginLeft="15dip"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:singleLine="false" android:paddingBottom="2dip" />
            </LinearLayout>
-1
source

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


All Articles