2016-11-11 4 views
1

Ich mache ein Layout:Wie füge ich Text darüber hinzu?

layout

ist hier ein 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="wrap_content" 
    android:background="#373c5c" 
    android:gravity="center_vertical" 
    android:orientation="vertical" > 




    <LinearLayout android:id="@+id/list_offer_item_container" 
    android:layout_marginTop="15dp" 
     android:layout_marginLeft="50dp" 
     android:layout_marginRight="50dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/shape" 
     android:orientation="horizontal" 
     android:padding="10dp"> 

     <ImageView 
      android:id="@+id/imgcreditcompany" 
      android:layout_width="36dp" 
      android:layout_marginRight="4dp" 
      android:layout_marginTop="0dp" 
      android:layout_marginBottom="0dp" 
      android:contentDescription="@string/app_name" 
      android:layout_height="36dp" /> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="match_parent" 
      android:gravity="start" 
      android:orientation="vertical" > 

      <TextView 
       android:id="@+id/txtname" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="8dp" 
       android:layout_marginTop="1dp" 
       android:textColor="#48899f" 
       android:textSize="@dimen/textsizeearncredit_title" 
       android:text="Name" 
       android:textAllCaps="false" 
       android:textStyle="normal|bold" /> 

      <TextView 
       android:id="@+id/txtdesc" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="8dp" 
       android:layout_marginTop="1dp" 
       android:maxLines="1" 
       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"/> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:layout_marginRight="20dp" 
      android:gravity="end" 

      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> 

     <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> 

Wie ein Text über die Schaltfläche Stil 'lineare Layout hinzufügen?

Ich möchte zum Beispiel "Hallo lieber Benutzer! Überprüfen Sie es unten".

Wo Textview, wo fügen Sie mehr Layout oder was auch immer?

+1

Können Sie genauer sein? Was "Button-Stil" lineares Layout? Welcher davon ist der Text, den Sie hinzufügen möchten? –

+0

@ KamilGryboś über diesem: @ + id/list_offer_item_container –

Antwort

0

Es ist sehr einfach! Sie müssen TextView mit der ID (text, zum Beispiel) hinzufügen, und in Ihrem LinearLayout müssen Sie die Zeile android:layout_below="@+id/text" hinzufügen. Optional können Sie das Attribut android:layout_alignParentTop="true" in TextView angeben.

Benutzen Sie einfach folgenden 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="wrap_content" 
       android:background="#373c5c" 
       android:gravity="center_vertical" 
       android:orientation="vertical" > 

    <TextView 
     android:id="@+id/text" 
     android:text="Text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     /> 


    <LinearLayout android:id="@+id/list_offer_item_container" 
        android:layout_below="@+id/text" 
        android:layout_marginTop="15dp" 
        android:layout_marginLeft="50dp" 
        android:layout_marginRight="50dp" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="@drawable/shape" 
        android:orientation="horizontal" 
        android:padding="10dp"> 

     <ImageView 
      android:id="@+id/imgcreditcompany" 
      android:layout_width="36dp" 
      android:layout_marginRight="4dp" 
      android:layout_marginTop="0dp" 
      android:layout_marginBottom="0dp" 
      android:contentDescription="@string/app_name" 
      android:layout_height="36dp" /> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="match_parent" 
      android:gravity="start" 
      android:orientation="vertical" > 

      <TextView 
       android:id="@+id/txtname" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="8dp" 
       android:layout_marginTop="1dp" 
       android:textColor="#48899f" 
       android:textSize="@dimen/textsizeearncredit_title" 
       android:text="Name" 
       android:textAllCaps="false" 
       android:textStyle="normal|bold" /> 

      <TextView 
       android:id="@+id/txtdesc" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="8dp" 
       android:layout_marginTop="1dp" 
       android:maxLines="1" 
       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"/> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:layout_marginRight="20dp" 
      android:gravity="end" 

      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> 

     <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> 
0

Above

<LinearLayout android:id="@+id/list_offer_item_container" 

setzen Sie Ihre TextView oder was auch immer Sie wollen. So etwas wie

<TextView android:id="@+id/myTV" 
    // other attributes /> 

Dann in <LinearLayout android:id="@+id/list_offer_item_container"

android:layout_below="@id/myTV"

standardmäßig hinzufügen, RelativeLayout setzt es Elemente in der oberen linken Ecke, bis Sie nichts anderes angeben. Wenn Sie das hinzufügen, wird Ihre LinearLayout unter die TextView gesetzt.

Ich denke, die Verwirrung ist auf, wie RelativeLayout und LinearLayout arbeiten. Sie haben orientation in Ihrem RelativeLayout was nichts tut. RelativeLayout legt seine Elemente in der Reihenfolge an, in der Sie sie anhand der Attribute wie layout_below und layout_above angeben. LinearLayout jedoch fügt sie in der Reihenfolge hinzu, in der sie in der XML-Datei angezeigt werden, weshalb orientation in LinearLayout, aber nicht RelativeLayout zählt.

Sie sollten sich die RelativeLayout.LayoutParams und andere Teile der documentation ansehen, um besser zu verstehen, wie die Layouts funktionieren und welche in Ihren speziellen Fällen am besten sind.

Verwandte Themen