سؤال

I am developping an application and currently working on the graphical interface. I would like to have the first imageView in the center of the screen. I used the attribute android:layout_gravity="center_horizontal" but I have the image appears in the left of the screen.

Is it because I defined a second LinearLayout that contains the ImageView and the textViews ? I defined it in order to define a weight for the text elements and a weight for the horizontal LinearLayout that contains the buttons.

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


<LinearLayout android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="4"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <ImageView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:src="@drawable/ebay"/> <!-- a modifier plus tard -->

    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_gravity="left"
        android:text="Book : "
        android:textSize="20sp"/>

    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:text="Sea change"
        android:textSize="25sp"/>

    <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_gravity="left"
        android:text="ISBN"
        android:textSize="20sp" />

    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:text="246546524"
        android:textSize="30sp"/>

</LinearLayout>

<LinearLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="horizontal">
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Cancel"/>
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="OK"/>
</LinearLayout>

How can I make it in the center of the screen then ?

هل كانت مفيدة؟

المحلول

Change your second layout width from wrap_content to match_parent:

<LinearLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="4"
    android:gravity="center_horizontal"
    android:orientation="vertical">

Here is your 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" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="4"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="20dp"
            android:src="@drawable/ebay" />
 <!-- a modifier plus tard -->

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_marginTop="20dp"
            android:text="Book : "
            android:textSize="20sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:text="Sea change"
            android:textSize="25sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_marginTop="20dp"
            android:text="ISBN"
            android:textSize="20sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:text="246546524"
            android:textSize="30sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Cancel" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="OK" />
    </LinearLayout>

Here is output screen: enter image description here

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top