Android 如何将图像视图居中放置在线性布局中?

Android 如何将图像视图居中放置在线性布局中?,android,Android,以下XML代码无法将图像视图居中放置在线性布局中: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageView

以下XML代码无法将图像视图居中放置在线性布局中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/myimg" 
        android:layout_width="36dip"
        android:layout_height="36dip"
        android:scaleType="fitXY"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/my_background"
    />
</LinearLayout>

这是一个非常有效的RelativeLayout代码:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/myimg" 
        android:layout_width="36dip"
        android:layout_height="36dip"
        android:scaleType="fitXY"
        android:layout_centerHorizontal="true"
        android:background="@drawable/my_background"
    />
</RelativeLayout>


有人能告诉我为什么吗?谢谢

问题是因为在LinearLayout中使用了android:layout\u width=“fill\u parent”。
将其更改为“包裹内容”

您必须在线性布局上设置垂直方向

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
    android:id="@+id/myimg" 
    android:layout_width="36dip"
    android:layout_height="36dip"
    android:scaleType="fitXY"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/my_background"
/>


通常对我有效的是,我将imageview包装在RelativeLayout中,然后进入LinearLayout,确保RelativeLayout的高度和宽度属性与LinearLayout匹配。您知道如何将imageview保持在相对视图的中心

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
              android:layout_width="fill_parent"
              android:layout_height="wrap_content">
              <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                         <ImageView 
                             android:id="@+id/myimg" 
                             android:layout_width="36dip"
                             android:layout_height="36dip"
                             android:scaleType="fitXY"
                             android:orientation="vertical"
                             android:layout_gravity="center_horizontal"
                             android:background="@drawable/my_background"
                         />
              </RelativeLayout>
    </LinearLayout>



将LinerLayout的android:gravity属性设置为中心。

对不起,上面的代码对我不起作用,所以我发布的代码对我有效

<?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="@color/colorPrimary"
    android:gravity="center_horizontal|center_vertical"
    android:orientation="vertical">

   <ImageView
        android:id="@+id/ivPic"
        android:layout_width="70dp"
        android:layout_height="70dp" />

</LinearLayout>


我希望它能帮助其他有需要的人。

您也可以使用框架布局和内部的线性布局使其更易于管理,更易于将其置于视图的中心

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout 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_gravity="center"
     android:orientation="vertical">

     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="@string/app_name" />

     <ImageView
         android:id="@+id/ivPic"
         android:layout_width="70dp"
         android:layout_height="70dp" />
 </LinearLayout>
</FrameLayout>


我知道您正在询问如何在布局中居中设置imageview,但希望提供另一种方法

让我们保持UI干净且易于理解

 <LinearLayout
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/phone_auth_bg"
        android:orientation="vertical">


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center">

            <ImageView
                android:id="@+id/imageMobile"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/phone" />

        </RelativeLayout>


如果需要更改图像部分中的任何属性,您可以进行更改,因为这不会导致布局UI中的任何更改。

请尝试线性布局中的布局重力和重力属性,使所有子对象默认居中

比如说,

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SplashActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000000"
        android:orientation="vertical"
        android:layout_gravity="center"
        android:gravity="center">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:foregroundGravity="center"
            app:srcCompat="@mipmap/app_icon_foreground">
        </ImageView>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Authenticator"
            android:textColor="#FFF"
            android:textSize="40dp"
            android:layout_margin="10dp">
        </TextView>
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>


以上是一个启动屏幕活动。所以有两个孩子——ImageView和TextView。两者都是中心对齐的。

我永远无法让重力属性正常工作。我只想使用相对布局。如果不缩放图像(scaleType),您可以通过将android:layout\u width=“36dip”更改为“fill\u parent”来实现。我在重力方面总是有问题。您可以尝试将layout_gravity属性放在父对象(LinearLayout)上,而不是放在ImageView上。这对我很有帮助。不幸的是,这使得只有包装:线性布局边界实际上与ImageView的边界相等(就可能的填充和边距而言),而不是将ImageView居中放置在更大的线性布局中。
android:gravity=“center”
应该像
center=center\u horizontal | center\u vertical
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SplashActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000000"
        android:orientation="vertical"
        android:layout_gravity="center"
        android:gravity="center">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:foregroundGravity="center"
            app:srcCompat="@mipmap/app_icon_foreground">
        </ImageView>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Authenticator"
            android:textColor="#FFF"
            android:textSize="40dp"
            android:layout_margin="10dp">
        </TextView>
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>