0

我正在开发一个应用程序,目前正在使用图形界面。我想在屏幕中央放置第一个 imageView。我使用了该属性android:layout_gravity="center_horizontal",但图像出现在屏幕左侧。

是因为我定义了一个LinearLayout包含 theImageViewtextViews 的秒吗?我定义它是为了定义文本元素的权重和LinearLayout包含按钮的水平线的权重。

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

那我怎样才能让它在屏幕的中心呢?

4

1 回答 1

1

将您的第二个布局宽度从更改wrap_contentmatch_parent

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

这是你的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>

这是输出屏幕:在此处输入图像描述

于 2014-03-17T11:12:48.183 回答