简体   繁体   中英

Android How to center the textview

I have tried tons of combinations, but my "sectionTitle textview" goes always to the left next to the "sectionOptionsButton button". I want to center it between both buttons horizontal and vertically.

<?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="fill_parent"
    android:background="@color/BackGroundColor"
    android:orientation="vertical"
    android:scaleType="center" >

    <LinearLayout
        android:id="@+id/mLlayout1"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:background="@color/BlueOcean"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/sectionOptionsButton"
            android:layout_width="33dp"
            android:layout_height="33dp"
            android:layout_weight="0"
            android:layout_gravity="left"            
            android:background="@drawable/adddatabase1"/>

        <TextView
            android:id="@+id/sectionTitle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:text="(blank)" />

        <Button
            android:id="@+id/addCatButton"
            android:layout_width="33dp"
            android:layout_height="33dp"
            android:layout_weight="0"
            android:layout_gravity="right"            
            android:background="@drawable/plusicon3"/>
    </LinearLayout>

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#b5b5b5"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/listview_style_list_selector" />

</LinearLayout>

Whats wrong? Thanks

layout_gravity only defines the position of your TextView in the parent LinearLayout . As your TextView width is set to match_parent , the TextView cannot be centered as you would expect.

Use gravity instead of layout_gravity . This will directly center the text in the TextView, not the TextView as a whole.

This is the full code:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:id="@+id/mLlayout1"
    android:layout_width="match_parent"
    android:layout_height="35dp"
    android:background="@color/BlueOcean"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/sectionOptionsButton"
        android:layout_width="33dp"
        android:layout_height="33dp"
        android:layout_weight="0"
        android:layout_gravity="left"            
        android:background="@drawable/adddatabase1"/>

    <TextView
        android:id="@+id/sectionTitle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:layout_weight="1"
        android:text="(blank)" />

    <Button
        android:id="@+id/addCatButton"
        android:layout_width="33dp"
        android:layout_height="33dp"
        android:layout_weight="0"
        android:layout_gravity="right"            
        android:background="@drawable/plusicon3"/>
</LinearLayout>

<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="#b5b5b5"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/listview_style_list_selector" />

try this way:

<LinearLayout
    android:id="@+id/mLlayout1"
    android:layout_width="match_parent"
    android:layout_height="35dp"
    android:background="@color/BlueOcean"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/sectionOptionsButton"
        android:layout_width="33dp"
        android:layout_height="33dp"
        android:layout_gravity="left"            
        android:background="@drawable/adddatabase1"/>

    <TextView
        android:id="@+id/sectionTitle"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:text="(blank)" />

    <Button
        android:id="@+id/addCatButton"
        android:layout_width="33dp"
        android:layout_height="33dp"
        android:layout_gravity="right"            
        android:background="@drawable/plusicon3"/>
</LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM