0

I have a Imagebutton, a horizontal Line which should be under the imagebutton and below this horizontal line I have a Textview.

Somehow, I can't center the Imagebutton, despite using android:gravity="center"

And also I can't set the horizontal line between Imagebutton and TextView. Please help

about.xml

     <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="32dp"
        android:background="@drawable/roundcorner"
        android:gravity="center"
        app:srcCompat="@drawable/ic_launcher" />


    <View
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:background="#c0c0c0"
        android:foregroundGravity="center_horizontal"
        android:layout_alignParentTop="true" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="1dp" />

</LinearLayout>
Blnpwr
  • 1,793
  • 4
  • 22
  • 43

2 Answers2

1

Use android:layout_gravity="center"instead of android:gravity="center" in the ImageButton.

Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
1

Working with Linearlayout , two attributes are for positioning of views-->

1) android:gravity
2) android:layout_gravity

Their name should help you :

android:gravity sets the gravity of the content of the View its used on.

android:layout_gravity sets the gravity of the View or Layout in its parent.

Shivam Sharma
  • 1,015
  • 11
  • 19