2016-05-05 5 views
2

Ich habe einige TextViews mit Zeichen auf der linken Seite. Layout:TextView set DrawableRight/Left ... Ausrichtung

  <android.support.v7.widget.CardView 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="@dimen/card_margin"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:padding="@dimen/detail_padding">     

       <TextView 
        android:id="@+id/location" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginBottom="@dimen/title_margin" 
        android:drawableLeft="@drawable/ic_location" 
        android:drawablePadding="@dimen/drawable_padding" 
        android:drawableStart="@drawable/ic_location" /> 

       <TextView 
        android:id="@+id/status" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginBottom="@dimen/title_margin" 
        android:drawableLeft="@drawable/ic_status" 
        android:drawablePadding="@dimen/drawable_padding" 
        android:drawableStart="@drawable/ic_status"/> 

       <TextView 
        android:id="@+id/description" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginBottom="@dimen/title_margin" 
        android:drawableLeft="@drawable/ic_description" 
        android:drawablePadding="@dimen/drawable_padding" 
        android:drawableStart="@drawable/ic_description"/> 
      </LinearLayout> 
     </android.support.v7.widget.CardView> 

Screenshot:

enter image description here

Wenn der Text zu groß ist, steht Symbol in der Mitte der Textview. Ich möchte das Symbol oben auf Textansicht ausrichten. Ist es möglich?

+0

posten Sie Ihre vollständigen xml Layout – Jeevanandhan

+0

Ich schlage vor, Sie auf diesen Link Antwort zu sehen ist hier http://stackoverflow.com/questions/10267352/align-top-of-image-to-top-of-textview/28906636 # 28906636] [1] –

+0

Ich möchte Zeichensatz auf die linke obere Kante von Textansicht –

Antwort

0

Sie müssen das Layout als RelativeLayout halten

<android.support.v7.widget.CardView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="@dimen/card_margin"> 
     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:padding="@dimen/detail_padding">     
      <TextView 
       android:id="@+id/location" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="@dimen/title_margin" 
       android:drawableLeft="@drawable/ic_location" 
       android:drawablePadding="@dimen/drawable_padding" 
       android:drawableStart="@drawable/ic_location" /> 
      <TextView 
       android:id="@+id/status" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="@dimen/title_margin" 
       android:drawableLeft="@drawable/ic_status" 
       android:drawablePadding="@dimen/drawable_padding" 
       android:drawableStart="@drawable/ic_status"/> 
      <TextView 
       android:id="@+id/description" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="@dimen/title_margin" 
       android:drawableLeft="@drawable/ic_description" 
       android:drawablePadding="@dimen/drawable_padding" 
       android:drawableStart="@drawable/ic_description"/> 
     </RelativeLayout> 
    </android.support.v7.widget.CardView> 
+0

und was es mir gibt? –

+0

Flexibilität zum Verschieben von Artikeln –

0

Sie

android:layout_toRightOf="@+id/imageId" 

auf der Textview einstellen und

android:layout_alignParentLeft="true" 

auf das Bild.

hoffe das hilft.

+0

danke. Ich werde das tun –

+0

Es löste Ihr Problem? Du hast das akzeptiert. Ich denke nicht, dass es für dich funktioniert. –

+0

Dies funktioniert mit relativem Layout. –

0

Verwenden Sie einfach Image

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:padding="@dimen/detail_padding"> 

    <TextView 
     android:id="@+id/location" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="@dimen/title_margin" 
     android:drawableLeft="@drawable/ic_location" 
     android:drawablePadding="@dimen/drawable_padding" 
     android:drawableStart="@drawable/ic_location" /> 

    <TextView 
     android:id="@+id/status" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="@dimen/title_margin" 
     android:drawableLeft="@drawable/ic_status" 
     android:drawablePadding="@dimen/drawable_padding" 
     android:drawableStart="@drawable/ic_status" /> 

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

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="@dimen/drawable_padding" 
      android:src="@drawable/ic_description" /> 

     <TextView 
      android:id="@+id/description" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="@dimen/title_margin" /> 
    </LinearLayout> 
</LinearLayout> 

0

Dafür tun müssen Sie eine benutzerdefinierte Drawable erstellen, die Ihre Drawable Wraps, und dann die Zeichnung der benutzerdefinierten Drawable manipulieren durch Überschreiben der Methode onDraw (Canvas).

Es gibt ein Beispiel unten. Dadurch wird das Bild nach oben ausgerichtet, Sie können es jedoch auch am unteren, linken oder rechten Rand von TextView ausrichten, indem Sie die erforderliche Logik in der onDraw (Canvas) -Methode implementieren. Vielleicht möchten Sie auch einen Rand in der OnDraw (Canvas) erstellen, um Ihre Design-Implementierung Pixel perfekt zu machen.

Verwendungsbeispiel:

GravityTopDrawable gravityDrawable = new GravityTopDrawable(innerDrawable); 
// NOTE: next 2 lines are important! 
innerDrawable.setBounds(0, 0, innerDrawable.getIntrinsicWidth(), innerDrawable.getIntrinsicHeight()); 
gravityDrawable.setBounds(0, 0, innerDrawable.getIntrinsicWidth(), innerDrawable.getIntrinsicHeight()); 
mTextView.setCompoundDrawables(gravityDrawable, null, null, null); 

Beispielcode:

public class GravityTopDrawable extends Drawable { 

    // inner Drawable 
    private final Drawable mDrawable; 

    public GravityTopDrawable(Drawable drawable) { 
     mDrawable = drawable; 
    } 

    @Override 
    public int getIntrinsicWidth() { 
     return mDrawable.getIntrinsicWidth(); 
    } 

    @Override 
    public int getIntrinsicHeight() { 
     return mDrawable.getIntrinsicHeight(); 
    } 

    @Override 
    public void draw(Canvas canvas) { 
     int halfCanvas= canvas.getHeight()/2; 
     int halfDrawable = mDrawable.getIntrinsicHeight()/2; 

     // align to top 
     canvas.save(); 
     canvas.translate(0, -halfCanvas + halfDrawable); 
     mDrawable.draw(canvas); 
     canvas.restore(); 
    } 
}