2015-10-29 4 views
6

Ich versuche, das Ergebnis wie in Pic1 zu erreichen. Versucht, Gravitation, Paddingleft usw. zu setzen, aber das Zeichenbild kommt am rechten Ende und nicht neben Text (siehe Bild 2 enter image description here). Kann jemand vorschlagen, wie man das Bild neben TextView ausrichtet? Wie kann man die Größe von Zeichen festlegen?Android: Zeichnet neben TextView mit Gewicht zu jedem TextView in LinearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
android:background="@drawable/separator" 
    android:padding="10dp"> 

<TextView 
    android:id="@+id/sms" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="2dp" 
    android:layout_marginRight="2dp" 
    android:layout_weight="1" 
    android:drawableEnd="@drawable/message" 
    android:drawableRight="@drawable/message" 
    android:gravity="center" 
    android:paddingLeft="3dp" 
    android:paddingRight="3dp" 
    android:text="SMS" /> 

<TextView 
    android:id="@+id/call" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:drawableEnd="@drawable/call" 
    android:drawableRight="@drawable/call" 
    android:gravity="center" 
    android:text="CALL" /> 

<TextView 
    android:id="@+id/email" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:gravity="center" 
    android:drawableEnd="@drawable/mail" 
    android:drawablePadding="3dp" 
    android:drawableRight="@drawable/mail" 
    android:text="MAIL" /> 

+0

Gewicht Textvorschau erzwingt, dass er die gesamte Breite einnimmt, daher wird das drwable an die Sackgasse der Textansicht gesetzt. – Dhina

+0

Benutze drawableLeft und setze das Bild ... Gib drawablePadding .. es wird perfekt sein – Prakhar

Antwort

0

eine benutzerdefinierte Schaltfläche erstellt, die das Layout Verschachtelungen erfordern tut und ziehbar Bilder je nach Bedarf ausrichten können.

Layout-Datei button.xml:

<Button xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/custom_button" 
    style="@style/custom_button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:drawablePadding="5dp" 
    android:gravity="center" /> 

Benutzerdefinierte Schaltfläche Klasse:

public class DrawableAlignedButton extends RelativeLayout { 

    private View view; 
    private Button button; 

    /** 
    * @param context 
    *   used to inflate the View. 
    * @param attrs 
    *   XML defined attributes. 
    */ 
    public DrawableAlignedButton(final Context context, final AttributeSet attrs) { 
    super(context, attrs); 
    init(context, attrs); 
    } 

    /** 
    * @param context 
    *   used to inflate the View. 
    */ 
    public DrawableAlignedButton(final Context context) { 
    super(context); 
    init(context, null); 
    } 

    /** 
    * @param context 
    *   used to inflate the View. 
    * @param attrs 
    *   XML defined attributes. 
    * @param style 
    *   the style for the View. 
    */ 
    public DrawableAlignedButton(final Context context, final AttributeSet attrs, final int style) { 
    super(context, attrs, style); 
    init(context, attrs); 
    } 

    private void init(final Context context, final AttributeSet attributeSet) { 
    view = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.button, this, true); 
    button = (Button) view.findViewById(R.id.custom_button); 

    String buttonText = null; 
    int drawableStart = 0; 
    int drawableEnd = 0; 

    if (attributeSet != null) { 
     final TypedArray a = context.getTheme().obtainStyledAttributes(attributeSet, R.styleable.CustomButtonStyle, 0, 0); 
     buttonText = a.getString(R.styleable.CustomButtonStyle_buttonText); 
     drawableStart = a.getResourceId(R.styleable.CustomButtonStyle_buttonDrawableStart, 0); 
     drawableEnd = a.getResourceId(R.styleable.CustomButtonStyle_buttonDrawableEnd, 0); 
     a.recycle(); 
    } 

    FontUtil.getInstance(context).useNormalRegularFont(button); 

    if (buttonText != null) { 
     button.setText(buttonText); 
    } 

    button.setCompoundDrawablesRelativeWithIntrinsicBounds(drawableStart, 0, drawableEnd, 0); 
    } 

    /** 
    * Sets the button text. 
    * 
    * @param text 
    *   the text to be set. 
    */ 
    public void setButtonText(final String text) { 
    if (button != null) { 
     button.setText(text); 
    } 
    } 

    /** 
    * Sets the drawable to the button. 
    * 
    * @param drawableStart 
    *   the drawable to set at the beginning of the text. 
    * @param drawableEnd 
    *   the drawable to set at the end of the text. 
    */ 
    public void setDrawableStart(final int drawableStart, final int drawableEnd) { 
    if (button != null) { 
     button.setCompoundDrawablesRelativeWithIntrinsicBounds(drawableStart, 0, drawableEnd, 0); 
    } 
    } 

} 

Wie es in der XML verwenden:

<com.package.view.DrawableAlignedButton 
     xmlns:drawableAlignedButton="http://schemas.android.com/apk/res-auto" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:background="@drawable/transparent_button_selector" 
     drawableAlignedButton:buttonDrawableStart="@drawable/small_active" 
     drawableAlignedButton:buttonText="Button Text" /> 
6

Versuchen, um dieses zu bauen.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:color/transparent" 
    android:orientation="horizontal" 
    android:padding="10dp"> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:layout_weight="1"> 

     <TextView 
      android:id="@+id/sms" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="2dp" 
      android:layout_marginRight="2dp" 
      android:gravity="center" 
      android:drawableRight="@drawable/message" 
      android:text="SMS" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:layout_weight="1"> 

     <TextView 
      android:id="@+id/call" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:drawableRight="@drawable/call" 

      android:gravity="center" 
      android:text="CALL" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:layout_weight="1"> 

     <TextView 
      android:id="@+id/email" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:drawableRight="@drawable/mail" 
      android:gravity="center" 
      android:text="MAIL" /> 
    </LinearLayout> 
</LinearLayout> 

enter image description here

+0

Ursprünglich dachte ich daran, es auf diese Weise zu machen, aber Layout zu vermeiden Verschachtelung versucht ohne verschachteltes Linearlayout. Ich werde Ihren Code verwenden, um das Ergebnis zu erhalten. Gibt es eine Möglichkeit, die Größe von Zeichen zu ändern? – kumar

+0

Größe des Zeichensatzes programmatisch auf Textansicht gesetzt? – Dhina

+0

Tab-Layout ist der bessere Ansatz als diese – rKrishna

0

Das Hauptproblem besteht darin, dass Sie die layoutWidth setzen = 0DP und Gewicht 1. Dies macht jede entsprechend dem Gewicht maximale Breite Textview zu nehmen. Mit drawableRight/drawableEnd wird die auszeichnungsfähige Ressource am äußersten rechten Rand der Ansicht festgelegt. Wenn du dieses Layout behalten möchtest, wickle es einfach in eine andere viewGroup wie @Dhinakaran gezeigt hat. Besser wäre es, ein Tab-Layout zu verwenden.

0

Anstatt mehrere lineare Layouts zu verwenden, können Sie ein relatives Layout verwenden.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 


    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:layout_gravity="bottom" 
     android:layout_weight=".33" 
     android:gravity="center_horizontal|center_vertical"> 

     <TextView 
      android:id="@+id/textView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_centerVertical="true" 
      android:text="Small Text" 
      android:textAppearance="?android:attr/textAppearanceSmall"/> 

     <ImageView 
      android:id="@+id/imageView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      android:layout_toEndOf="@+id/textView" 
      android:layout_toRightOf="@+id/textView" 
      android:src="@android:drawable/btn_star"/> 
    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:layout_gravity="bottom" 
     android:layout_weight=".33" 
     android:gravity="center_horizontal|center_vertical"> 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_centerVertical="true" 
      android:text="Small Text" 
      android:textAppearance="?android:attr/textAppearanceSmall"/> 

     <ImageView 
      android:id="@+id/imageView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      android:layout_toEndOf="@+id/textView2" 
      android:layout_toRightOf="@+id/textView2" 
      android:src="@android:drawable/btn_star"/> 
    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:layout_gravity="bottom" 
     android:layout_weight=".33" 
     android:gravity="center_horizontal|center_vertical"> 

     <TextView 
      android:id="@+id/textView3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_centerVertical="true" 
      android:text="Small Text" 
      android:textAppearance="?android:attr/textAppearanceSmall"/> 

     <ImageView 
      android:id="@+id/imageView3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      android:layout_toEndOf="@+id/textView3" 
      android:layout_toRightOf="@+id/textView3" 
      android:src="@android:drawable/btn_star"/> 
    </RelativeLayout> 
</LinearLayout> 

Ergebnis

enter image description here