Android 如何在 LinearLayout 中将 ImageView 居中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3293517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:32:17  来源:igfitidea点击:

How to center an ImageView in a LinearLayout?

android

提问by user256239

The following XML code doesn't work to center an ImageView in a LinearLayout:

以下 XML 代码无法在 LinearLayout 中将 ImageView 居中:

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

And this is RelativeLayout code which surprisingly works:

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

Can someone tell me why? Thanks!

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

回答by Pajeh

You have to set vertical orientation on your LinearLayout

您必须在 LinearLayout 上设置垂直方向

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

回答by ULHAS PATIL

Sorry, but above code didn't work for me, so i posting my Code which worked for me is

抱歉,但上面的代码对我不起作用,所以我发布了对我有用的代码

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

I hope it helps for others who need.

我希望它对需要的人有所帮助。

回答by rahul

What usually works for me is, I wrap the imageview inside a RelativeLayout when then goes into a LinearLayout making sure that height and width attributes of the relativelayout matches the linearLayout. And you know how to keep the imageview in the center of the relativelayout.

通常对我有用的是,当我进入一个 LinearLayout 时,我将 imageview 包裹在一个 RelativeLayout 中,确保relativelayout 的高度和宽度属性与线性布局相匹配。而且您知道如何将图像视图保持在相对布局的中心。

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

回答by Peety

<LinearLayout
    android:orientation="horizontal"  
    android:layout_width="0dp"
    android:layout_weight="0.50"
    android:gravity="center"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/addToDateOrReceipt"
        android:layout_width="25dp"
        android:layout_height="25dp" />
</LinearLayout>

Set the android:gravity property of your LineraLayout to center.

将 LineraLayout 的 android:gravity 属性设置为 center。

回答by lujop

The problem is because you use android:layout_width="fill_parent" in LinearLayout. Change it to "wrap_content"

问题是因为您在 LinearLayout 中使用了 android:layout_width="fill_parent"。将其更改为“wrap_content”

回答by Avinash Singh

let's keep the UI clean and simple to understand.

让我们保持 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>

And if any attributes need to be changed in the image-section, you may bring the change as it won't cause any changes in the layout UI.

如果需要更改图像部分中的任何属性,您可以带来更改,因为它不会导致布局 UI 发生任何更改。

回答by Sergio

You also can made it with a FrameLayout and inside of it a LinearLayout so it is easier to manage and easier to put it on the center of the view.

您还可以使用 FrameLayout 并在其内部使用 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>

I know you are asking about how to center an imageview in a layout but wanted to provide another way to do it

我知道你在问如何在布局中居中图像视图,但想提供另一种方法来做到这一点