2014-03-18 7 views
3

Невозможно определить способ иметь центрированный TextView с вертикально центрированными горизонтальными линиями с обеих сторон, растянутыми влево и вправо от экрана. Как на картинке ниже.Горизонтальные линии с обеих сторон TextView

That's what I'm talking about

Любое понимание поможет!

ответ

5

Наконец пришел с этим решением, которое делает именно то, что требуется

<FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="24dp" 
      > 

     <View 
       android:layout_width="match_parent" 
       android:layout_height="1dp" 
       android:background="@color/light_grey" 
       android:layout_gravity="center_vertical"/> 

     <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/add_heading_description" 
       android:textSize="@dimen/add_headings" 
       android:textColor="@color/dark_grey" 
       android:paddingLeft="10dp" 
       android:paddingRight="10dp" 
       android:layout_gravity="center" 
       android:background="@color/white" 
       /> 

    </FrameLayout> 
1

Это, как я это делаю:

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginTop="20dp" 
     android:orientation="horizontal" 
     android:padding="5dp" > 

     <View 
      android:layout_width="90dp" 
      android:layout_height="1dp" 
      android:layout_marginTop="12dp" 
      android:background="#999999" /> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:text="DESCRIPTION" 
       android:textColor="#000000" 
       android:textSize="20sp" 
       android:textStyle="bold"/> 

     </LinearLayout> 

     <View 
      android:layout_width="wrap_content" 
      android:layout_height="1dp" 
      android:layout_marginTop="12dp" 
      android:background="#999999" /> 
    </LinearLayout> 

</LinearLayout> 

Я знаю, что это не идеальный ответ, но может дать вам идею.

Вот как его ищет:

Test Image http://imageshack.com/a/img854/9623/exly.jpg

+0

Это не центрирования TextView, не так ли? Вам нужно изменить ширину 90dp с каждым изменением размера текста/шрифта, чтобы он выглядел так, как будто он центрирован? – Leo

+0

Правда, как я уже упоминал, этот ответ не идеален. Просто, хотя и достиг этих линий. Все, что будет лучше, тоже будет приятным. – mike20132013

+0

ОК, просто проверял, правильно ли я вас понял. Было бы здорово найти способ действительно сосредоточить текст ... – Leo

-1

Это, как я это сделал, и он выглядит точно так же, как я разыскивается.

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_marginRight="24dp" 
    android:layout_marginLeft="24dp" 
    android:layout_marginTop="8dp"> 

    <View 
     android:layout_gravity="center_vertical" 
     android:layout_width="0dp" 
     android:layout_height="1dp" 
     android:layout_weight="1" 
     android:background="#FFF" /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="or" 
     android:textColor="#FFF" 
     android:paddingLeft="16dp" 
     android:paddingRight="16dp" /> 
    <View 
     android:layout_gravity="center_vertical" 
     android:layout_width="0dp" 
     android:layout_height="1dp" 
     android:layout_weight="1" 
     android:background="#FFF" /> 
</LinearLayout> 
+0

Это было бы ОК, если бы вы хотели иметь что-то подобное на одном экране. Представьте, что вам нужно иметь его на экранах более 5+. Что тогда? Ну, а предложения были бы: пользовательский макет и использование '' '' или создание '' CustomTextView'' с слегка измененным методом '' onDraw (Canvas canvas) ''. Я сам выбрал №2. Ура! – Toochka

1

У меня возникли проблемы с принятым ответ, так что делюсь своим путем:

<LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_marginTop="0dp" 
      android:orientation="horizontal"> 

      <View 
       android:layout_width="40dp" 
       android:layout_height="1dp" 
       android:background="@color/secondary_text" 
       android:layout_gravity="center_vertical" 
       android:layout_weight="1"/> 

      <Button 
       android:layout_width="20dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:background="@null" 
       android:text="@string/Or" 
       android:clickable="false" 
       android:textColor="@color/secondary_text" /> 

      <View 
       android:layout_width="40dp" 
       android:layout_height="1dp" 
       android:background="@color/secondary_text" 
       android:layout_gravity="center_vertical" 
       android:layout_weight="1"/> 

     </LinearLayout> 

enter image description here

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