我有一张图片想放在屏幕中央。尽管出乎我的意料,它是水平居中而不是垂直居中。也就是说,图像触及屏幕顶部。
相同的布局在沙盒项目中显示正常,但不幸的是在真实项目中却不行。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!-- ... -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/progress2"
android:src="@drawable/wait2"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
线条
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
当然是不必要的,但是这两种情况都不起作用。
欢迎提出任何建议。
编辑:从 InputMethodService
显示 View 似乎是相关的。
最佳答案
我刚刚遇到了同样的问题。我用过
android:layout_centerInParent
它适用于 AndroidStudio 预览版和带有 4.3 设备的 Nexus 4。但在 Galaxy S1 2.3 上它不起作用。
我通过将我想居中的 Layout 包装在 RelativeLayout 中并使用 gravity 属性居中来修复它:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<RelativeLayout
...
这在两种设备上都有效。
关于android:layout_centerInParent 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16209607/