2016-02-25 3 views
-1

У меня есть XML-файл с RelativeLayout, у меня также есть Textview с Gravity Center. В коде я хотел бы получить координату X этого Textview, где начинается TextView (длина TextView не постоянна, она может измениться). Мне нужно, чтобы ImageView был рядом с TextView.Как сделать ImageView слева от TextView, когда Textview является HorizontalCentered

<TextView 
     android:id="@+id/textView6" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_alignParentTop="true" 
     android:gravity="center" 
     android:text="TextView" /> ` 

    countryText = (TextView) findViewById(R.id.textView6); 
    countryText.getLeft() 

getLeft() return 0; 

ответ

0

Добавить андроид: layout_centerInParent = "истина" в TextView и добавить андроид: layout_toLeftOf = "@ идентификатор/textView6" в ImageView

0

Просто избавиться от ImageView и добавить соединения вытяжка к слева от вашего TextView.
Это также более эффективно, поскольку оно помогает сгладить иерархию раскладки.

т.е .:

<TextView 
    android:id="@+id/textView6" 
    android:drawableLeft="@drawable/my_icon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_alignParentTop="true" 
    android:gravity="center" 
    android:text="TextView" 
/> 

Официальный docs