2015-05-23 23 views
6

vorrei sapere come fare questa linea orizzontale con il testo in mezzo, guarda questo screenshot:Android: linea orizzontale con il testo in mezzo

enter image description here

Qualcuno ha un'idea di farlo su Android ? Ho trovato come fare una linea orizzontale, ma mai con il testo.

Grazie!

risposta

30

Basta cambiare i colori da abbinare a quelli sulla vostra immagine. Ti suggerisco anche di usare una sfumatura come sfondo per quelle viste fittizie, sembra molto meglio dello screenshot se ci metti un po 'di tempo.

<RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true"> 

     <TextView 
      android:id="@+id/tvText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:text="lala" 
      android:textColor="#FFFFFF"/> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="1dp" 
      android:layout_centerVertical="true" 
      android:layout_marginLeft="16dp" 
      android:layout_toLeftOf="@id/tvText" 
      android:background="#FF0000" 
      /> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="1dp" 
      android:layout_centerVertical="true" 
      android:layout_marginRight="16dp" 
      android:layout_toRightOf="@id/tvText" 
      android:background="#FF0000" 
      /> 

    </RelativeLayout> 

enter image description here

+0

Thx! Funziona perfettamente;) Non riesco ad aggiungere 1 scusa, ma tutto è ok per me! – Asfi

+0

@Asfi sarà presto in grado, penso che hai solo bisogno di 15 punti per +1 post :) Nessun problema tra –

+0

Nel caso in cui alcune persone hanno errore, non dimenticare di dichiarare namespace in RelativeLayout: 'xmlns: android =" http://schemas.android.com/apk/res/android "' – StevenTB

0

In Your xml

android:layout_width="fill_parent" 
android:layout_height="2dip" 
android:background="#FF00FF00" /> 

o, in codice -

View ruler = new View(myContext); ruler.setBackgroundColor(0xFF00FF00); 
theParent.addView(ruler, 
new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 2)); 
+0

Questa non è la mia domanda, io so come fare. Voglio aggiungere del testo nel mezzo di questa riga. Grazie. – Asfi

+0

quindi crea un'immagine e mettila su backgrounf di textview il soln più semplice..e rendi trasparente textview – Tufan

+0

amico questa è un'immagine statica che puoi fare in quel modo o fare a modo mio quello che ho postato – Tufan

0

Prova questa

impostare la larghezza della visualizzazione di conseguenza

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


      <View 
      android:id="@+id/topDivider" 
      android:layout_width="50dp" 
      android:layout_height="1dp" 
      android:layout_below="@id/internal" 
      android:background="@color/bright_blue"/> 

      <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:text="Middle text here" 
      android:layout_gravity="center_horizontal" 
      android:id="@+id/lv_shopName" /> 


      <View 
      android:id="@+id/topDivider" 
      android:layout_width="50dp" 
      android:layout_height="1dp" 
      android:layout_below="@id/internal" 
      android:background="@color/bright_blue"/> 


      </LinearLayout> 
0
<LinearLayout 
    android:id="@+id/linerlayout" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 

    <View 
     android:layout_marginRight="2dp" 
     android:id="@+id/divider" 
     android:layout_width="wrap conent" 
     android:layout_height="1dp" 

     android:background="#00000000" /> 

    <TextView 
     android:id="@+id/ll" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     Text="abc"/> 

<View 
     android:layout_marginLeft="2dp" 
     android:id="@+id/divider" 
     android:layout_width="wrap conent" 
     android:layout_height="1dp" 

     android:background="#00000000" /> 
3
public class CenterLineTextView extends android.support.v7.widget.AppCompatTextView { 

private final Rect mBounds = new Rect(); 
private final Paint mPaint = new Paint(); 
private int mPadding; 
private int mStroke; 

public CenterLineTextView(Context context) { 
    super(context); 
    init(); 
} 

public CenterLineTextView(Context context, @Nullable AttributeSet attrs) { 
    super(context, attrs); 
    init(); 
} 

public CenterLineTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
    init(); 
} 

private void init() { 
    if (isInEditMode()) { 
     return; 
    } 
    setGravity(Gravity.CENTER); 
    mStroke = getContext().getResources().getDimensionPixelSize(R.dimen.divider); 
    mPadding = getContext().getResources().getDimensionPixelSize(R.dimen.login_or_padding); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    mPaint.setStrokeWidth(mStroke); 
    mPaint.setColor(getPaint().getColor()); 
    getPaint().getTextBounds(getText().toString(), 0, getText().length(), mBounds); 
    canvas.drawLine(0, getHeight()/2, (getWidth() - mBounds.right)/2 - mPadding, getHeight()/2, mPaint); 
    canvas.drawLine(mPadding + (getWidth() + mBounds.right)/2, getHeight()/2, getWidth(), getHeight()/2, mPaint); 
} 
} 
+1

puoi offrire qualche spiegazione –

Problemi correlati