Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

java - Android Textview is moved to right

This is what I actually have as a result: enter image description here

As you can see value to right of Current HP is moved to right instead to stay close to "Current HP" textview like the rest of values. Why is that so? This is my code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:background="@color/semiTrans"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/llHeader"
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@id/text"
            android:layout_marginRight="5dp"
            app:srcCompat="@android:drawable/ic_dialog_info" />

        <TextView
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:gravity="center_vertical|center_horizontal"
            android:text="CONTINUE INFO"
            android:textColor="#000"
            android:textSize="25sp" />


    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl2"
        android:layout_marginHorizontal="10dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/llHeader"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true">


        <TextView
            android:id="@+id/currentHp"
            android:textStyle="bold"
            android:layout_width="168dp"
            android:layout_height="wrap_content"
            android:text="Current HP: "
            android:textSize="18sp"/>

        <TextView
            android:id="@+id/nextHp"
            android:textStyle="bold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/currentHp"
            android:text="Next +10HP: "
            android:textSize="18sp"/>

        <TextView
            android:id="@+id/fullHp"
            android:textStyle="bold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/nextHp"
            android:text="HP fully recovered: "
            android:textSize="18sp"/>

        <TextView
            android:id="@+id/currentHpValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toEndOf="@+id/currentHp"
            android:layout_toRightOf="@+id/currentHp"
            android:text="a"
            android:textSize="18sp"/>

        <TextView
            android:id="@+id/nextHpValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/currentHp"
            android:layout_toEndOf="@+id/nextHp"
            android:layout_toRightOf="@+id/nextHp"
            android:text="a"
            android:textSize="18sp"/>

        <TextView
            android:id="@+id/fullHpValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/fullHp"
            android:layout_alignBottom="@+id/fullHp"
            android:layout_toEndOf="@+id/fullHp"
            android:layout_toRightOf="@+id/fullHp"
            android:text="a"
            android:textSize="18sp"/>

        <TextView
            android:id="@+id/tvAdditionalInfo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_below="@id/fullHp"
            android:textSize="18sp"
            android:layout_centerHorizontal="true"
            android:text="Additional info"/>

    </RelativeLayout>

    <Button
        android:id="@+id/dialogButtonOK"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rl2"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginHorizontal="10dp"
        android:layout_marginBottom="10dp"
        android:background="@drawable/button"
        android:text=" Ok " />

</RelativeLayout>

I was trying to solve this by setting padding and margin to 0 but it did not work. I do not know what else Ishould added.

question from:https://stackoverflow.com/questions/65849120/android-textview-is-moved-to-right

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I found a solution. I need to set currentHp textview width to wrap content.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...