JDJ
JDJ

Reputation: 4328

How do you horizontally position a TextView at a certain percent of the width from one side?

I'm trying to set my TextView's center to be positioned at 25% of the width from the left side of its parent layout.

Here is what it looks like now:

(A)

------------------------------------------
|             Text                       | 
------------------------------------------
          ^          ^          ^         
         25%        50%        75% 

Here is what I'd like it to look like:

(B)

------------------------------------------
|        Text                            |
------------------------------------------
          ^          ^          ^         
         25%        50%        75% 

Here is the code I have now that is producing (A):

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="1"
    android:layout_gravity="center"
    android:gravity="center_vertical|center_horizontal" >

    <LinearLayout 
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5">

        <TextView 
            android:id="@+id/textView"      
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center_horizontal"
            android:textSize="16sp"
            android:text="text" />

    </LinearLayout>

</LinearLayout>

Here is a screenshot of what it looks like now:

actionbar with textview

Updated screenshot:

enter image description here

Upvotes: 0

Views: 98

Answers (2)

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

You need to remove the gravity and layout_gravityof your outer LinearLayout and in the inner LinearLayout you need to set the gravity to right with layout_weight of 0.25 for 25%

sample:

EDIT:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="horizontal"
    android:weightSum="1" >

<View
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="0.12" >
</View>

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="text"
    android:textSize="16sp" />

</LinearLayout>

Upvotes: 1

TheRealBo
TheRealBo

Reputation: 73

try my code buddy, i think thats what u want

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="1"
     >
    <TextView 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:gravity="center"
        android:text="text"
        />
</LinearLayout>

Upvotes: 1

Related Questions