The following XML code doesn't work to center an ImageView in a LinearLayout:
以下XML代码无法将ImageView集中在LinearLayout中:
<?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!
有人可以告诉我为什么吗?谢谢!
6 个解决方案
#1
29
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"
/>
#2
19
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.
我希望它有助于其他需要的人。
#3
7
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.
通常对我有用的是,我将imageview包装在RelativeLayout中,然后进入LinearLayout,确保relativelayout的height和width属性与linearLayout匹配。并且您知道如何将imageview保持在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>
#4
2
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”
#5
2
<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属性设置为居中。
#6
0
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
我知道你在询问如何将一个imageview集中在一个布局中,但想提供另一种方法来实现它
#1
29
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"
/>
#2
19
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.
我希望它有助于其他需要的人。
#3
7
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.
通常对我有用的是,我将imageview包装在RelativeLayout中,然后进入LinearLayout,确保relativelayout的height和width属性与linearLayout匹配。并且您知道如何将imageview保持在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>
#4
2
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”
#5
2
<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属性设置为居中。
#6
0
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
我知道你在询问如何将一个imageview集中在一个布局中,但想提供另一种方法来实现它