Android 安卓:将图像居中
我有一个线性布局和一个图像Android 安卓:将图像居中,android,android-layout,Android,Android Layout,我有一个线性布局和一个图像 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Ima
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:layout_width="wrap_content" android:id="@+id/imageView1"
android:src="@drawable/icon" android:layout_height="wrap_content"
android:scaleType="center"></ImageView>
</LinearLayout>
如何动态居中图像,使其显示在所有设备的屏幕中央?在
LinearLayout
中,使用:android:layout\u gravity=“center”
在
RelativeLayout
中,使用:android:layout\u centerInParent=“true”
只需将其添加到ImageView中即可
android:layout_gravity="center"
从技术上讲,上述两个答案都是正确的,但由于您将ImageView设置为
fill\u parent
而不是wrap\u content
,因此其中的图像不居中,但ImageView本身居中
为ImageView提供以下属性:
android:scaleType="centerInside"
android:gravity="center"
只有在这种情况下,如果图像超过ImageView的大小,才需要scaleType。您可能还需要不同的scaleType。总之,
android:gravity
是您在本例中要寻找的,但是如果您的ImageView设置为wrap\u content
,您应该将ImageView的gravity(android:layou gravity
)设置为在父视图的中心。如果您使用的是线性布局
,使用gravity
属性:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView android:layout_width="wrap_content"
android:id="@+id/imageView1"
android:src="@drawable/icon"
android:layout_height="wrap_content"
android:scaleType="centerInside" />
</LinearLayout>
另一种方法。(在相对测试中,但我认为在线性测试中也可以)
如果使用Eclipse,则可以在*.xml文件处于活动状态时选择图形布局。在顶部,可以找到结构和选项“调整视图边界”。它会将伪帧(蓝色矩形)的所有尺寸缩短到可绘制文件的大小
另请参见带有“使图像有趣”的scaleType选项。在Eclipse中尝试;) 这对我有用
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/Logo"
android:gravity="center"
android:scaleType="centerInside"
android:src="@drawable/logo" />
</LinearLayout>
试试这个
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:scaleType="centerInside"
android:src="@drawable/logo" />
</LinearLayout>
这对我来说是什么
LinearLayout
RelativeLayout
<ImageView
width...
height..
---> android:layout_centerHorizontal="true"
线性布局
相对布局
android:layout\u centerHorizontal=“true”
干杯 你也可以用这个
android:layout_centerHorizontal="true"
图像将放置在屏幕中央
更改布局重量,根据您将获得的
输入以下内容:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.03">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="centerInside"
android:layout_gravity="center"
android:src="@drawable/logo" />
</LinearLayout>
这里有两种方法可以使图像在线性布局中垂直和水平居中 (1) 在ImageView中使用android:layout\u gravity=“center”属性
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
layout_height="wrap_content"
android:src="@drawable/icon"
android:layout_gravity="center"/>
</LinearLayout>
工作原理:
ImageView中的android:layout_gravity=“center”属性相对于其父对象(LinearLayout)在垂直方向和水平方向上居中
--或--
(2) 或者,您可以在LinearLayout中使用android:gravity=“center”属性,并在ImageView中省略android:layout\u gravity=“center”属性:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
layout_height="wrap_content"
android:src="@drawable/icon"
android:layout_gravity="center"/>
</LinearLayout>
工作原理:LinearLayout centers中的android:gravity=“center”属性其子项(在本例中为图像)垂直和水平首先,您需要在LinearLayout声明中使用'match\u parent'而不是'fill\u parent',您可以检查“官员 “文档”在这里 另一个观察结果是,您需要将ImageView作为子级使用,然后使用ImageView应该是自动关闭的;这是 意味着它应该以“/>”结尾。 然后让我给你展示一些快速的想法: 1-作为自然方式
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
><ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/imageView1"
android:src="@drawable/ocean" android:scaleType="center"/> </LinearLayout>
那么看起来
2-你可以快速即兴创作
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
/><ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView1"
android:scaleType="center"/><ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView1"
android:src="@drawable/ocean"
android:scaleType="center"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView1"
android:background="#ffffff"
android:scaleType="center"/></LinearLayout>
然后看起来像
3-其他想法也可能以这种方式诞生,例如这一个
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
><ImageView
android:layout_width="300dp"
android:layout_height="80dp"
android:id="@+id/imageView1"
/><ImageView
android:layout_width="300dp"
android:layout_height="80dp"
android:id="@+id/imageView1"
android:src="@drawable/ocean"
/>
<ImageView
android:layout_width="300dp"
android:layout_height="80dp"
android:id="@+id/imageView1"
/> </LinearLayout>
像这样闲逛
其他可能性
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
><ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ocean"
android:id="@+id/imageView1"
android:scaleType="center"/><ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView1"
android:background="#ffffff"
android:scaleType="center"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView1"
android:background="#ffffff"
android:src="@drawable/ocean"
android:scaleType="center"/></LinearLayout>
正如我们所看到的,您可以使用“隐藏空间”或“带背景色的空间”快速解决一些问题,显然,这并不总是可能的,android:scaleType=“centerInside”-->这条线用于将图像居中,但您需要将图像的高度和宽度作为背景
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
><ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ocean"
android:id="@+id/imageView1"
android:scaleType="center"/><ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView1"
android:background="#ffffff"
android:scaleType="center"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView1"
android:background="#ffffff"
android:src="@drawable/ocean"
android:scaleType="center"/></LinearLayout>
<ImageView
android:id ="@+id/imageView6"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:scaleType ="centerInside"
android:layout_marginTop ="20dp"
app:srcCompat ="@drawable/myimage" />
<或代码>只把它放在“顶端”,我希望它在x轴和y轴的中间居中。如果你想把它放在中间,你就不能使用线性布局。这是用来排列列表中的东西。相反,使用相对论。这只是把它放在“顶部”。“我希望它以X和Y轴为中心。”SkigIT:我建议使用<代码> RealValayOut< <代码>,而不是<代码>线性布局< /代码>。+ 1用于ReleValayOUT,使用:Android:LayOutCultCynPrime=“true”< Calp> RealValayOut< <代码>帮助>代码> RealValayOut还有两个选项:水平布局和垂直布局。我很惊讶没有人投票支持这个答案,因为这是唯一对我有效的答案。谢谢感谢您的贡献,但请让您的答案更具描述性,使其更易于理解。仅显示代码对许多人没有帮助。尝试给出一些内联注释或解释解决方案的工作原理和原因:)
<ImageView
android:id ="@+id/imageView6"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:scaleType ="centerInside"
android:layout_marginTop ="20dp"
app:srcCompat ="@drawable/myimage" />