Center a button in a Linear layout

后端 未结 19 1277
遇见更好的自我
遇见更好的自我 2020-12-04 04:55

I am using a linear layout to display a pretty light initial screen. It has 1 button that is supposed to centre in the screen both horizontally and vertically. However no

相关标签:
19条回答
  • 2020-12-04 05:33

    If you want to center an item in the middle of the screen don't use a LinearLayout as these are meant for displaying a number of items in a row.

    Use a RelativeLayout instead.

    So replace:

    android:layout_gravity="center_vertical|center_horizontal"
    

    for the relevant RelativeLayout option:

    android:layout_centerInParent="true"
    

    So your layout file will look like this:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout android:id="@+id/RelativeLayout01" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        xmlns:android="http://schemas.android.com/apk/res/android">
    
        <ImageButton android:id="@+id/btnFindMe" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@drawable/findme"></ImageButton>
    
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-12-04 05:34

    Have you tried defining android:gravity="center_vertical|center_horizontal" inside the layout and setting android:layout_weight="1" in the image?

    0 讨论(0)
  • 2020-12-04 05:37

    A commonly-used method that works with linear layout is to set a property on the image button

    android:layout_gravity="center"
    

    You can choose whether to left-align, center-align, or right-align each object in the linear layout. Note that the above line is precisely the same as

    android:layout_gravity="center_vertical|center_horizontal"
    
    0 讨论(0)
  • 2020-12-04 05:37

    Add this

    android:gravity="center"
    

    In LinearLayout.

    0 讨论(0)
  • 2020-12-04 05:37

    Did you try this?

      <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainlayout" android:orientation="vertical"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5px"
    android:background="#303030"
    >
        <RelativeLayout
        android:id="@+id/widget42"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        >
        <ImageButton
        android:id="@+id/map"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="map"
        android:src="@drawable/outbox_pressed"
        android:background="@null"
        android:layout_toRightOf="@+id/location"
        />
        <ImageButton
        android:id="@+id/location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="location"
        android:src="@drawable/inbox_pressed"
        android:background="@null"
        />
        </RelativeLayout>
        <ImageButton
        android:id="@+id/home"
        android:src="@drawable/button_back"
        android:background="@null"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        />
    </RelativeLayout>
    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5px"
    android:orientation="horizontal"
    android:background="#252525"
    >
        <EditText
        android:id="@+id/searchfield"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="@drawable/customshape"
        />
        <ImageButton
        android:id="@+id/search_button"
        android:src="@drawable/search_press"
        android:background="@null"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        />
    </LinearLayout>
    <com.google.android.maps.MapView
        android:id="@+id/mapview" 
        android:layout_weight="1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:clickable="true"
        android:apiKey="apikey" />
    
    </TableLayout>
    
    0 讨论(0)
  • 2020-12-04 05:38

    Center using a LinearLayout:

    <LinearLayout
        android:id="@+id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical" >
    
        <ImageButton
            android:id="@+id/btnFindMe"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/findme" />
    </LinearLayout>
    
    0 讨论(0)
提交回复
热议问题