简体   繁体   中英

Android - Center View in Other View inside Layout

Hi I am having trouble centering my text view in an image view. I want it to look kinda like this but have it actually vertically centered and stay vertically centered if the the font size changes.

在此处输入图片说明

And here is what I have in XML I don't care if I need to change the relative layout to another kind that doesn't matter to me I just want to be able to center this. Heres the xml:

<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.spencer.app.ProfileActivity"
android:id="@+id/layout">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:minHeight="140dp"
    android:minWidth="140dp"
    android:id="@+id/logoBackground"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="25dp"
    android:layout_marginStart="25dp"
    android:layout_marginTop="25dp"
    android:contentDescription="@string/content_description_useless"
    android:background="#ffffd0cd" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/capital_u"
    android:id="@+id/u_textView"
    android:textSize="80sp"
    android:layout_marginLeft="31dp"
    android:layout_marginStart="31dp"
    android:layout_marginTop="20dp"
    android:layout_marginBottom="20dp"
    android:layout_alignTop="@+id/logoBackground"
    android:layout_alignLeft="@+id/logoBackground"
    android:layout_alignStart="@+id/logoBackground" />

</RelativeLayout>

Thanks for the help in advance.

try this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.spencer.app.ProfileActivity" >

    <ImageView
        android:id="@+id/logoBackground"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="25dp"
        android:layout_marginStart="25dp"
        android:layout_marginTop="25dp"
        android:background="#ffffd0cd"
        android:contentDescription="@string/content_description_useless"
        android:minHeight="140dp"
        android:minWidth="140dp" />

    <TextView
        android:id="@+id/u_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/logoBackground"
        android:layout_alignLeft="@+id/logoBackground"
        android:layout_alignRight="@+id/logoBackground"
        android:layout_alignStart="@+id/logoBackground"
        android:layout_alignTop="@+id/logoBackground"
        android:text="@string/capital_u"
        android:textSize="80sp"
        android:gravity="center" />

</RelativeLayout>

Your issue is that you're not centering a view INSIDE of something else, you're just trying to draw on top.

One possible solution would be to set the width and height of the TextView to be the same as the dimensions of the ImageView . Then, add this to your TextView 's XML:

android:gravity="center"

But overall, this is NOT the right way to do this sort of stuff. You'd be much better off just changing the background of your TextView with something like this:

android:background="#ffffd0cd"

You can also change the background to be a drawable:

android:background="@drawable/my_logo_background_image"
<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:id="@+id/layout"
tools:context="com.spencer.app.ProfileActivity">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:minHeight="140dp"
    android:minWidth="140dp"
    android:id="@+id/logoBackground"
    android:layout_marginStart="25dp"
    android:background="#ffffd0cd"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="U"
    android:id="@+id/u_textView"
    android:layout_marginTop="30dp"
    android:layout_marginLeft="45dp"
    android:textSize="80sp"
    android:layout_alignTop="@+id/logoBackground"
    android:layout_alignLeft="@+id/logoBackground"
    android:layout_alignStart="@+id/logoBackground" />

I didnot changed much but Its work. Replace your code with this it will work for you. :)

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