2013-11-19 4 views
0

У меня есть текстовое представление, настроенное для отображения целого числа. Диапазон от 0 до 255. Текстовое изображение находится прямо над стрелкой, поэтому я хочу, чтобы он всегда был центрирован. Я пробовал центр тяжести, но это не применимо. Я также установил textview для отображения пользовательского шрифта, поэтому я не уверен, влияет ли это на параметр центра тяжести. С текущим кодом текстовое представление кажется оправданным.текстовый центр не работает

  <TextView 
    android:id="@+id/textViewR" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="25dp" 
    android:layout_marginTop="10dp" 
    android:gravity="center" 
    android:text="255" 
    android:textSize="37dp" 
    android:textColor="#FF00FF00" /> 

отобразить значение

 ViewGreen.setText(" " + G_value); 

Вот файл XML

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<SeekBar 
    android:id="@+id/seekBarr" 
    android:layout_width="300dp" 
    android:layout_height="70dp" 
    android:layout_marginLeft="-100dp" 
    android:layout_marginTop="180dp" 
    android:progressDrawable="@drawable/mastersliderback" 
    android:rotation="270" 
    android:thumb="@drawable/imgsliderred70" /> 

<SeekBar 
    android:id="@+id/seekBarg" 
    android:layout_width="300dp" 
    android:layout_height="70dp" 
    android:layout_marginLeft="-10dp" 
    android:layout_marginTop="180dp" 
    android:progressDrawable="@drawable/mastersliderback" 
    android:rotation="270" 
    android:thumb="@drawable/imgslidergreen70" /> 

<SeekBar 
    android:id="@+id/seekBarb" 
    android:layout_width="300dp" 
    android:layout_height="70dp" 
    android:layout_marginLeft="80dp" 
    android:layout_marginTop="180dp" 
    android:progressDrawable="@drawable/mastersliderback" 
    android:rotation="270" 
    android:thumb="@drawable/imgsliderblue70" /> 


<!-- Here is the textView I am editing to make the center apply --> 
<TextView 
    android:id="@+id/textViewR" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:layout_marginLeft="25dp" 
    android:layout_marginTop="10dp" 
    android:background="#FF000000" 
    android:text="255" 
    android:textColor="#FF00FF00" 
    android:textSize="38dp" /> 

<TextView 
    android:id="@+id/textViewG" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="110dp" 
    android:layout_marginTop="10dp" 
    android:text="255" 
    android:textColor="#FF00FF00" 
    android:textSize="38dp" /> 

<TextView 
    android:id="@+id/textViewB" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="200dp" 
    android:layout_marginTop="10dp" 
    android:text="255" 
    android:textColor="#FF00FF00" 
    android:textSize="38dp" /> 

Скриншот dimmer

Final рабочего XML

 <TextView 
    android:id="@+id/textViewR" 
    android:layout_width="73dp" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="18dp" 
    android:layout_marginTop="10dp" 
    android:gravity="center" 
    android:text="255" 
    android:textColor="#FF00FF00" 
    android:textSize="37dp" /> 

ответ

0

android:gravity="center" центрирует содержимое внутри View.

Использование android:layout_gravity="center" центрировать View внутри его parent

<TextView 
    android:id="@+id/textViewR" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="25dp" 
    android:layout_marginTop="10dp" 
    android:gravity="center" 
    android:layout_gravity="center_horizontal" // or just center, whatever you need 
    android:text="255" 
    android:textSize="37dp" 
    android:textColor="#FF00FF00" /> 

Docs

+0

Я попробовал center_horizontal и его все еще оставил оправданным. Я также удалил пространство в ViewGreen.setText ("" + G_value); между "". – Bobby

+0

Удалите свой «marginLeft» и посмотрите, что он делает. – codeMagic

+0

помещает текст в левую часть окна по-прежнему Оправданно слева. FYI Я также удалил шрифт, но все еще не центрирован. – Bobby

0

Тейк TextView внутри LinearLayout и дать центр тяжести в этой LinearLayout.

0

Я внесла необходимые изменения, установив Textview на 3 символа в ширину. Я опубликовал изменения.

Смежные вопросы