1

Ich möchte etwas Text über das Bild setzen. Kann mir bitte jemand dabei helfen?Wie setze ich Text über mehrere Bilder in einem LinearLayout?

Ich habe sechs rechteckige Box und ich möchte etwas Text auf jeder Box setzen. Ich habe ImageView verwendet, um die Rechtecke zu machen.

finden Sie die unten mainactivity.xml Code:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#ffffff" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    tools:context="com.example.vimal.edkul.StudentProfile"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/layout1"> 
     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="@color/title"> 
      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="left" 
       android:src="@drawable/pic13" /> 
      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:text="User Profile" 
       android:textSize="20sp" 
       android:textColor="@android:color/white" /> 
      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="right" 
       android:src="@drawable/ic_action_overflow" /> 
     </android.support.v7.widget.Toolbar> 
    </LinearLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/layout2" 
     android:layout_gravity="center" 
     android:layout_below="@+id/layout1" 
     android:padding="30dp"> 
     <FrameLayout 
      android:layout_height="100dp" 
      android:layout_width="130dp"> 
      <View 
       android:background="@drawable/rectangle" 
       android:clickable="false" 
       android:id="@+id/view1" 
       android:layout_height="match_parent" 
       android:layout_width="match_parent" /> 
      <TextView 
       android:gravity="center" 
       android:id="@+id/text1" 
       android:textColor="#000" 
       android:layout_height="match_parent" 
       android:layout_width="match_parent" 
       android:text="Some text" /> 
     </FrameLayout> 
     <FrameLayout 
      android:layout_height="100dp" 
      android:layout_width="130dp"> 
      <View 
       android:background="@drawable/rectangle" 
       android:clickable="false" 
       android:id="@+id/view2" 
       android:layout_height="match_parent" 
       android:layout_width="match_parent" /> 
      <TextView 
       android:gravity="center" 
       android:id="@+id/text2" 
       android:textColor="#000" 
       android:layout_height="match_parent" 
       android:layout_width="match_parent" 
       android:text="Some text" /> 
     </FrameLayout> 
    </LinearLayout> 
</RelativeLayout> 

rectangle.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
    <solid android:color="@android:color/transparent"/> 
    <corners android:radius="12px"/> 
    <stroke android:width="2dip" android:color="#000000"/> 
</shape> 
+0

Verwenden framelayout anstelle von linearlayout –

+0

@ payaltuteja danke für die antwort. Ich bin neu bei android wenn ich framelayout so wie kann ich text über ein bild kann mir ein beispiel geben bitte das wird mir helfen, besser zu verstehen. –

+0

Möchten Sie Text in Bitmaps von Bildern einfügen? Oder Platzieren von Text in Standard-TextViews und das Platzieren dieser TextViews vor ImageViews reicht aus? –

Antwort

2

Sie können den folgenden Teil des Layouts verwenden, anstatt jedes ImageView:

<FrameLayout 
    android:layout_height="150dp" 
    android:layout_width="100dp" > 

    <View 
     android:background="@drawable/rectangle" 
     android:clickable="false" 
     android:id="@+id/buttonVote" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" /> 

    <TextView 
     android:gravity="center" 
     android:id="@+id/textStatusMessage" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:text="Some text" /> 

</FrameLayout> 

EDIT

Sie können Hintergrund in rectangle.xml ändern. Zum Beispiel:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <solid android:color="@android:color/darker_gray" /> 
    <corners android:radius="12px" /> 
    <stroke 
     android:width="2dip" 
     android:color="#000000" /> 
</shape> 

Der xml looks that für mich.

Sie auch FrameLayout und View und setzen backgound für die Textview löschen:

<TextView 
     android:id="@+id/text1" 
     android:layout_width="150dp" 
     android:layout_height="100dp" 
     android:gravity="center" 
     android:text="Some text" 
     android:textColor="#000" 
     android:background="@drawable/rectangle"/> 

P. S. Um das einfache Bild zu zeigen, brauchen Sie ImageView nicht zu verwenden, einfaches View mit dem Hintergrund ist genug.

+0

Danke für die Hilfe. Es hat funktioniert, aber ich bin nicht in der Lage, Hintergrundfarbe.Ich habe Erwähnen Sie etwas Farbe in rectanga.xml in einem drawbaren Ordner. Aber es erscheint nicht auf dem Bildschirm. –

+0

Vielleicht müssen Sie View zu ImageView mit 'src' Parameter ändern. Aber das sieht komisch aus. Können Sie Ihre 'rectangle.xml' in Ihren Fragetext posten? Und, wenn du kannst, bitte aktualisiere ein ganzes XML, wie hast du es geändert? –

+0

Ich habe meinen Code mit new activity.xml und rectangle.xml aktualisiert. –

Verwandte Themen