【问题标题】:How to display image in center across resolutions如何在不同分辨率的中心显示图像
【发布时间】:2014-12-24 20:47:20
【问题描述】:

我有一个布局,其中一个内部不位于中心,它只发生在少数分辨率设备中,而在少数设备中它完全位于中心。下面是代码和截图

<ImageView android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:gravity="center"
                android:src="@drawable/thank" android:paddingTop="10dip"
                android:paddingBottom="10dip"/>

代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/splash_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:gravity="center" android:orientation="vertical"
    android:background="@drawable/background">

    <ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:gravity="center_horizontal"
        android:src="@drawable/logo" android:paddingTop="10dip"
        android:paddingBottom="10dip" />

     <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <ImageView android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:gravity="center"
            android:src="@drawable/thank" android:paddingTop="10dip"
            android:paddingBottom="10dip"/>

        <com.teleca.sam.ui.Copyright
            android:layout_width="fill_parent" android:layout_height="fill_parent"
            android:gravity="bottom|center"/>
    </RelativeLayout>
</LinearLayout>

【问题讨论】:

  • 你可以使用单个RelativeLayout

标签: android xml layout alignment


【解决方案1】:

RelativeLayout 可能是这个原因。改用线性布局。

【讨论】:

    【解决方案2】:

    如果您使用的是LinearLayout

    <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:layout_height="wrap_content"
            android:id="@+id/imageView"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>
    

    如果您使用的是RelativeLayout

    <RelativeLayout 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="wrap_content"
            android:id="@+id/imageView"
            android:src="@drawable/ic_launcher"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true" />
    </RelativeLayout>
    

    【讨论】:

      【解决方案3】:

      因为你的 ImageView 在一个 RelativeLayout,你应该使用

      android:layout_centerInParent="true"
      

      而不是

      android:gravity="center"
      

      在您的 ImageView 标记中

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-17
        • 1970-01-01
        • 1970-01-01
        • 2015-02-23
        • 1970-01-01
        相关资源
        最近更新 更多