2016-11-15 1 views
0

Ich habe ein kleines Problem.Wie TextView mit Hintergrund im Layout ausrichten - Android

Ich habe:

enter image description here

Ich möchte: (aber der Text einige dp von links)

enter image description here

Ist es möglich? Hier ist ein ganzer Code meines Layouts: (@ + id/txtName ist die Textview aus dem Screenshot!)

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/md_brown_100" 
    android:orientation="vertical" 
    android:gravity="center" 
    android:paddingTop="10dp" 
    android:paddingBottom="10dp" > 


    <LinearLayout android:id="@+id/list_offer_item_container" 
     android:layout_width="150dp" 
     android:layout_height="150dp" 
     android:background="@drawable/shape" 
     android:gravity="center" 
     android:paddingTop="10dp" 
     android:paddingBottom="10dp" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true"> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:gravity="center" 

      android:layout_gravity="center_vertical"> 

      <TextView android:id="@+id/list_offer_badge_text" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_above="@+id/list_offer_item_container" 
       android:textColor="@color/md_amber_700" 
       android:textSize="12sp" 
       android:visibility="gone" 
       android:text=""/> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_height="150dp" 
     android:orientation="vertical" 
     android:padding="5dp" 
      android:weightSum="1" 
      android:layout_width="150dp"> 

      <ImageView 
       android:id="@+id/imgcreditcompany" 
       android:scaleType="fitXY" 
       android:layout_gravity="center_horizontal" 

       android:layout_above="@+id/txtname" 
        android:contentDescription="@string/app_name" 
       android:layout_height="48dp" 
       android:layout_width="48dp" /> 


      <TextView 
       android:id="@+id/txtname" 
       android:gravity="center_vertical" 
       android:background="@drawable/ic_dollar_ss" 
       android:layout_height="30dp" 
       android:layout_marginTop="5dp" 
       android:textColor="@color/AliceBlue" 
       android:textSize="18sp" 
       android:text="Name" 
       android:textAllCaps="false" 
       android:textStyle="normal|bold" 
       android:layout_width="153dp" /> 

      <TextView 
       android:id="@+id/txtdesc" 
       android:layout_height="wrap_content" 
       android:gravity="center_horizontal" 
       android:layout_marginTop="1dp" 
       android:maxLines="3" 

       android:textColor="#80869c" 
       android:textSize="@dimen/textsizeearncredit_desc" 
       android:text="This is a description of the offer and this is just a demo to show off 3 lines stacking correctly on top of each other" 
       android:layout_weight="0.55" 
       android:layout_width="140dp" /> 
     </LinearLayout> 

     <ImageView 
      android:id="@+id/nextArrow" 
      android:layout_alignParentRight="true" 
      android:tint="@color/md_grey_300" 
      android:rotation="180" 
      android:layout_width="32dp" 
      android:visibility="gone" 
      android:layout_marginRight="16dp" 
      android:layout_gravity="center" 
      android:layout_centerVertical="true" 
      android:layout_height="32dp" 
      android:contentDescription="@string/app_name" 
      android:padding="@dimen/two" /> 

    </LinearLayout> 


</RelativeLayout> 

Bitte helfen Sie uns, Jungen! Vielen Dank. Du kannst mir eine Idee davon geben. Prost!

Antwort

1

Das Problem ist, dass Ihr Linearlayout eine Polsterung aus 5DP hat, die davon entfernt Ihre „Freunde einladen“ Textview hält vom Rande .

ersetzen

<LinearLayout 
     android:layout_height="150dp" 
     android:orientation="vertical" 
     android:padding="5dp" 
     android:weightSum="1" 
     android:layout_width="150dp"> 

mit

<LinearLayout 
     android:layout_height="150dp" 
     android:orientation="vertical" 
     android:paddingTop="5dp" 
     android:paddingBottom="5dp" 
     android:paddingEnd="5dp" 
     android:weightSum="1" 
     android:layout_width="150dp"> 

So kann man auf der linken Seite keine Polsterung haben. Um es im Beschreibungstext zu kompensieren, fügen Sie Ihrem TextView txtdesc

+0

Große Lösung! Danke, aber wie schafft man den Hintergrund an dem Ort, den ich will, und den Text ein paar dp von der linken Grenze? –

+0

Überprüfen Sie den Kommentar über –

+0

Sie können hinzufügen, 'android: paddingStart' zu Ihrem LinearLayout und legen Sie fest, wie viele DP Sie möchten, dass es vom linken Rand entfernt. Beachten Sie, dass Sie das ursprüngliche Ergebnis erhalten, wenn Sie es auf 5dp setzen. Was passiert mit dem Hintergrund? –

0

Obwohl ich in Android Studio ziemlich neu bin, würde ich empfehlen, die Layout-Gravity-Eigenschaft auf diesem bestimmten TextView zu ändern, um sich an die linke Ecke anzupassen und/oder auch ein Padding nach links zu erzwingen.

Obwohl dies nur eine Vermutung von dem, was es lösen könnte, ich hoffe, es

+0

des txtview? android:paddingStart="5dp" hinzu?Es funktioniert nicht. –

0

Sets Layout Schwere dieser Ansicht android:layout_gravity="left"

+0

des txtview? Es funktioniert nicht. –

0

Lösung hilft: --- Bitte fügen Sie im Namen Textview folgende Eigenschaft. android:gravity="center_horizontal"

Code:

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:gravity="center" 
android:orientation="vertical" 
android:paddingBottom="10dp" 
android:paddingTop="10dp"> 


<LinearLayout 
    android:id="@+id/list_offer_item_container" 
    android:layout_width="150dp" 
    android:layout_height="150dp" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:gravity="center" 
    android:paddingBottom="10dp" 
    android:paddingTop="10dp"> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:gravity="center" 

     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/list_offer_badge_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_above="@+id/list_offer_item_container" 
      android:text="" 
      android:textSize="12sp" 
      android:visibility="gone" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="150dp" 
     android:layout_height="150dp" 
     android:orientation="vertical" 
     android:padding="5dp" 
     android:weightSum="1"> 

     <ImageView 
      android:id="@+id/imgcreditcompany" 
      android:layout_width="48dp" 
      android:layout_height="48dp" 
      android:layout_above="@+id/txtname" 
      android:layout_gravity="center_horizontal" 
      android:contentDescription="@string/app_name" 
      android:scaleType="fitXY" /> 


     <TextView 
      android:id="@+id/txtname" 
      android:layout_width="153dp" 
      android:layout_height="30dp" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginTop="5dp" 
      android:gravity="center_horizontal" 
      android:text="Name" 
      android:textAllCaps="false" 
      android:textSize="18sp" 
      android:textStyle="normal|bold" /> 

     <TextView 
      android:id="@+id/txtdesc" 
      android:layout_width="140dp" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="1dp" 
      android:layout_weight="0.55" 

      android:gravity="center_horizontal" 
      android:maxLines="3" 
      android:text="This is a description of the offer and this is just a demo to show off 3 lines stacking correctly on top of each other" 
      android:textColor="#80869c" /> 
    </LinearLayout> 

    <ImageView 
     android:id="@+id/nextArrow" 
     android:layout_width="32dp" 
     android:layout_height="32dp" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:layout_gravity="center" 
     android:layout_marginRight="16dp" 
     android:contentDescription="@string/app_name" 
     android:rotation="180" 
     android:visibility="gone" /> 

</LinearLayout> 

0

Das padding-Attribut für das lineare Layout den Text und Bildansicht erstreckt, sollte

android:padding="5dp" 

Dazu gehören Polsterungen auf 0 gesetzt werden links & rechts, daher die lila Farbe hat eine kleine Lücke, entfernen Sie die Polsterung und die lila Form wird die erreichen Seiten der Containerform.

Ich bemerkte, dass Sie nur wollen, dass die lila Form die RHS berührt, daher möchten wir nur die paddingLeft entfernen.

android:paddingRight="5dp" 
    android:paddingLeft="0dp" 
    android:paddingBottom="5dp" 
    android:paddingTop="5dp" 

die Schwerkraft des Textview Attribut muss dann auf ‚links‘

eingestellt wird
android:gravity="left" 
+0

Tolle Lösung! Danke, aber wie schafft man den Hintergrund an dem Ort, den ich will, und den Text ein paar dp von der linken Grenze? –

+0

@DawidWalczyk fügen Sie paddingLeft zum TextView hinzu, stimmen Sie bitte meine Antwort ab, wenn es half. Ihre Begrüßung. – RamanSB

Verwandte Themen