2014-07-10 3 views
0

J'ai deux TextViews alignés dans la même rangée. Si le texte dans les deux TextViews est petit, ils correspondent à la même ligne dans l'écran. Mais si le texte à gauche TextView (employee name) augmente, le texte dans le droit TextView (employee type) est déplacé vers la deuxième ligne, mais sur le côté droit de l'écran. Comment puis-je faire le deuxième TextView aligné à gauche et dans la deuxième ligne (ci-dessous employee name) si le employee name dans gauche TextView est augmenté et les deux TextViews ne rentrent pas dans la même rangée?Alignement de TextViews dans la même rangée

S'il vous plaît vérifier la ci-dessous capture d'écran: current behaviour

S'il vous plaît trouver mon code ci-dessous:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="#fff" 
    android:orientation="horizontal" 
    android:padding="10dp" > 

<TextView 
    android:id="@+id/employee_name" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="22.0sp" /> 

<TextView android:id="@+id/employee_type" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingLeft="8dp" 
    android:layout_toRightOf="@id/employee_name" 
    android:layout_alignBaseline="@id/employee_name" 
    android:textSize="15.0sp" /> 

</RelativeLayout> 
+0

donnent des lignes vraies et max ligne simple 1 dans la mise en page – Nava2011

+0

Pourquoi utilisez-vous 2 Textviews? – PKlumpp

+0

@ Nava2011: merci pour votre contribution. c'est emballer le texte dans une ligne. mais existe-t-il un moyen de passer à la deuxième ligne juste en dessous de la première vue de texte. J'ai besoin d'afficher le texte entier. – void

Répondre

0

La réponse est d'utiliser une seule TextView, et de modifier les caractéristiques en utilisant Span.

En supposant que vous aviez seulement le TextView avec @+id/employee_name:

TextView employeeInfo = (TextView) findViewById(R.id.employee_name); 

String employeeName = // get your employee name 
String employeeType = // get your employee type 

// Assuming you move your 15sp into a dimens.xml resource file 
int employeeTypeSize = getResources().getDimensionPixelSize(R.dimen.employee_type_text_size); 

SpannableStringBuilder builder = new SpannableStringBuilder(employeeName); 
builder.append(employeeType); 

// Set the smaller text size from the end of the name to the end 
// of the string. 
builder.setSpan(new AbsoluteSizeSpan(employeeTypeSize), 
       employeeName.length(), builder.length(), 
       Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 

// Then set this spannable to the TextView 
employeeInfo.setText(builder); 
1

Je propose une des opérations suivantes:

  1. Placez toujours le type d'employé au deuxième ligne, pour éviter des problèmes comme celui-ci.
  2. Trouvez une autre façon d'indiquer l'employé qui n'implique pas d'épeler le type. Par exemple, utilisez peut-être une sorte de symbole qui ne prend pas autant de place. De cette façon, même si le nom devient très long, vous pouvez toujours laisser place à un petit symbole.
0

Essayez ceci:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="#fff" 
    android:weightSum="2" 
    android:orientation="horizontal" 
    android:orientation="horizontal" 
    android:padding="10dp" > 

<TextView 
    android:id="@+id/employee_name" 
    android:layout_width="match_parent" 
    android:weightSum="2" 
    android:weight="1" 
    android:layout_height="wrap_content" 

    android:textSize="22.0sp" /> 

<TextView android:id="@+id/employee_type" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:weight="1" 
    android:paddingLeft="8dp" 
    android:textSize="15.0sp" /> 

</LinearLayout> 
Questions connexes