it-swarm.com.de

Android Button Platziere das Bild in der Mitte und den Text unten

Ich möchte Image in der Mitte platzieren und Text direkt darunter in einem Button. Ich habe versucht, Android einzustellen: Schwerkraft, konnte es aber nicht richtig einstellen, hier ist das Image:

enter image description here

unten ist mein xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android" 
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    Android:orientation="vertical"
    Android:paddingLeft="10.0dip" 
    Android:paddingTop="10.0dip" 
    Android:paddingRight="10.0dip" 
    Android:paddingBottom="10.0dip"    
    Android:background="@drawable/background_img"
    >

    <LinearLayout
        Android:layout_height="0dp"
        Android:layout_width="match_parent"
        Android:layout_weight="1"
        Android:orientation="horizontal"
        Android:paddingLeft="10.0dip" 
        Android:paddingTop="10.0dip" 
        Android:paddingRight="10.0dip" 
        Android:paddingBottom="10.0dip"          
        >

        <Button          
            Android:layout_height="match_parent"
            Android:layout_width="0dp"
            Android:layout_weight="1"
            Android:id="@+id/button_listen"
            Android:text="@string/listen"
            Android:drawableTop="@drawable/listen_btn"
            Android:layout_marginLeft="10dp"
            Android:layout_marginRight="10dp"
            Android:layout_marginTop="10dp"
            Android:layout_marginBottom="10dp"      
            Android:layout_gravity="center"
            />
        <Button
            Android:layout_height="match_parent"
            Android:layout_width="0dp"
            Android:layout_weight="1"
            Android:id="@+id/button_gallery"
            Android:text="@string/gallery"
            Android:drawableTop="@drawable/gallery_btn"
            Android:layout_marginLeft="10dp"    
            Android:layout_marginRight="10dp"    
            Android:layout_marginTop="10dp"    
            Android:layout_marginBottom="10dp"   
            Android:layout_gravity="center"
            />
    </LinearLayout>
    <LinearLayout
        Android:layout_height="0dp"
        Android:layout_width="match_parent"
        Android:layout_weight="1"
        Android:orientation="horizontal"
        Android:paddingLeft="10.0dip" 
        Android:paddingTop="10.0dip" 
        Android:paddingRight="10.0dip" 
        Android:paddingBottom="10.0dip"              
        >

        <Button
            Android:layout_height="match_parent"
            Android:layout_width="0dp"
            Android:layout_weight="1"
            Android:id="@+id/button_play"
            Android:text="@string/play"                
            Android:drawableTop="@drawable/play_btn"
            Android:layout_marginLeft="10dp"
            Android:layout_gravity="center"
            Android:layout_marginRight="10dp"
            Android:layout_marginTop="10dp"
            Android:layout_marginBottom="10dp"
            />
        <Button
            Android:layout_height="match_parent"
            Android:layout_width="0dp"
            Android:layout_weight="1"
            Android:id="@+id/button_find"
            Android:text="@string/find"
            Android:drawableTop="@drawable/test_btn"
            Android:layout_marginLeft="10dp"
            Android:layout_marginRight="10dp"
            Android:layout_marginTop="10dp"    
            Android:layout_marginBottom="10dp" 
            Android:layout_gravity="center"
            />        
    </LinearLayout>
</LinearLayout>

Wie zentriere ich das Bild und den Text darunter?

Vielen Dank im Voraus.

14
RDX

Versuchen Sie dies für mich

<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"
    tools:context=".MainActivity" >
<Button
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"
        Android:layout_alignParentBottom="true"
        Android:layout_centerHorizontal="true"
        Android:layout_marginBottom="151dp"
        Android:onClick="onClick"
        Android:drawableTop="@drawable/ic_launcher"
        Android:text="Button" />
</RelativeLayout>
18
DjHacktorReborn

Ändern Sie die Schaltfläche in LinearLayout und verwenden Sie den gleichen Onclicklistener für den Fake-Button

    <LinearLayout
        Android:id="@+id/fakeButton"
        Android:layout_width="0dp"
        Android:layout_height="fill_parent"
        Android:layout_weight="1"
        Android:gravity="center_vertical|center_horizontal"
        Android:orientation="vertical" >

        <ImageView
            Android:layout_width="wrap_content"
            Android:layout_height="wrap_content"
            Android:src="@drawable/icon_big_evento" />

        <TextView
            Android:layout_width="wrap_content"
            Android:layout_height="wrap_content"
            Android:text="@string/menu_about" />
    </LinearLayout>
2
Victor Pinto

Ich folgte den Kommentaren von DjHacktorReborn und machte das Layout, hier ist das eingefrorene

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    Android:background="@drawable/background_img"
    Android:orientation="vertical" >
  <LinearLayout
        Android:layout_width="match_parent"
        Android:layout_height="match_parent"
        Android:layout_weight="1"
        Android:orientation="vertical"
        Android:weightSum="10" >    
      <LinearLayout
            Android:layout_width="match_parent"
            Android:layout_height="match_parent"
            Android:layout_weight="4.1"
            Android:orientation="vertical" >
        </LinearLayout>        
        <TableLayout
                Android:layout_width="match_parent"
                Android:layout_height="match_parent"
                Android:layout_weight="2"
                Android:orientation="horizontal"
                Android:stretchColumns="*" >    
                 <TableRow Android:layout_weight="1" >
                      <RelativeLayout
                        Android:layout_width="match_parent"
                        Android:layout_height="match_parent" >

                        <Button
                            Android:id="@+id/button_listen"
                            Android:layout_width="match_parent"
                            Android:layout_height="match_parent"
                            Android:layout_centerInParent="true"
                            Android:background="@drawable/main_list"

                            Android:paddingTop="90dp"



                            Android:text="@string/listen"
                            Android:textStyle="bold"
                            Android:textColor="#FFFFFF" />

                        <ImageView
                            Android:id="@+id/main_icon_content_image_view"
                            Android:layout_width="wrap_content"
                            Android:layout_height="wrap_content"
                            Android:layout_centerInParent="true"
                            Android:src="@drawable/listen_btn" />
                    </RelativeLayout>
                     <RelativeLayout
                        Android:layout_width="match_parent"
                        Android:layout_height="match_parent" >

                        <Button
                            Android:id="@+id/button_gallery"
                            Android:layout_width="match_parent"
                            Android:layout_height="match_parent"
                            Android:background="@drawable/main_list"

                            Android:paddingTop="90dp"


                            Android:text="@string/gallery"
                             Android:textStyle="bold"
                            Android:textColor="#FFFFFF" />

                        <ImageView
                            Android:id="@+id/main_icon_schedule_image_view"
                            Android:layout_width="wrap_content"
                            Android:layout_height="wrap_content"
                            Android:layout_centerInParent="true"
                            Android:src="@drawable/gallery_btn" />
                    </RelativeLayout>
                      </TableRow>
                      <TableRow Android:layout_weight="1" >

                    <RelativeLayout
                        Android:layout_width="match_parent"
                        Android:layout_height="match_parent" >

                        <Button
                            Android:id="@+id/button_play"
                            Android:layout_width="match_parent"
                            Android:layout_height="match_parent"
                            Android:background="@drawable/main_list"
                            Android:ellipsize="Marquee"

                            Android:paddingTop="90dp"


                            Android:text="@string/play"
                             Android:textStyle="bold"
                            Android:textColor="#FFFFFF"
                             />

                        <ImageView
                            Android:layout_width="wrap_content"
                            Android:layout_height="wrap_content"
                            Android:layout_centerInParent="true"
                            Android:src="@drawable/play_btn" />
                    </RelativeLayout>

                    <RelativeLayout
                        Android:layout_width="match_parent"
                        Android:layout_height="match_parent" >

                        <Button
                            Android:id="@+id/button_find"
                            Android:layout_width="match_parent"
                            Android:layout_height="match_parent"
                            Android:background="@drawable/main_list"

                            Android:paddingTop="90dp"


                            Android:text="@string/find"
                             Android:textStyle="bold"
                            Android:textColor="#FFFFFF" />

                        <ImageView

                            Android:layout_width="wrap_content"
                            Android:layout_height="wrap_content"
                            Android:layout_centerInParent="true"
                            Android:src="@drawable/test_btn" />
                    </RelativeLayout>


                </TableRow>
    </TableLayout>                      
         <LinearLayout
            Android:layout_width="match_parent"
            Android:layout_height="match_parent"
            Android:layout_weight="4.1"
            Android:orientation="vertical" >
        </LinearLayout>        
</LinearLayout>
</LinearLayout>

Hier ist das aktualisierte Ergebnis

enter image description here

Danke für die Vorschläge

0
RDX