【问题标题】:Image won't center with android:layout_centerHorizontal图像不会以 android:layout_centerHorizo​​ntal 居中
【发布时间】:2016-07-18 06:27:01
【问题描述】:

我的图像不会使用代码“android:layout_centerHorizo​​ntal”居中,即使使用重力也无法解决它。有没有其他方法或者我的相对布局和滚动视图中有错误的输入。

scale layout bounds landscape view of with scale layout bounds 屏幕尺寸 768x1280 nexus api23

我在使用 android 版本 4 时没有问题,也许这与 api 相关?因为我的最低 sdk 是 android 版本 2?

代码如下:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:fillViewport="true"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.capstone.jmilibraryapp.Login">


<EditText
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:id="@+id/etUsername"
    android:hint="Enter Sr-Code"
    android:layout_below="@+id/imageView3"
    android:layout_centerHorizontal="true" />
<ImageView
    android:layout_centerHorizontal="true"
    android:layout_width="500dp"
    android:layout_height="200dp"
    android:src="@drawable/jmilogo"
    android:id="@+id/imageView3"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<EditText
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:id="@+id/etPassword"
    android:inputType="textPassword"
    android:hint="Password"
    android:layout_below="@+id/etUsername"
    android:layout_centerHorizontal="true" />

<Button
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:id="@+id/bLogin"
    android:text="Log In"
    android:layout_below="@+id/etPassword"
    android:layout_alignLeft="@+id/etPassword"
    android:layout_alignStart="@+id/etPassword" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Log In as Guest"
    android:id="@+id/guest"
    android:layout_marginTop="20dp"
    android:layout_below="@+id/bLogin"
    android:layout_centerHorizontal="true" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Log In as Guest"
    android:id="@+id/testinguser"
    android:layout_marginTop="20dp"
    android:layout_below="@+id/guest"
    android:layout_centerHorizontal="true"
   />


</RelativeLayout>
</ScrollView>

【问题讨论】:

  • 你想要什么??我的意思是你想把你的 imageview 放在 EditText 后面??
  • @uttampanchasara 先生,我添加了一张图片,抱歉回复晚了,我家里的网速很慢。

标签: android-layout android-xml


【解决方案1】:

可能您的图像与 ImageView 的比例不同,您可以在设备的开发人员工具中检查它是否启用 showLayoutBounds。您将看到 imageView 的实际大小。

要解决这个问题,您可以使 ImageView 与您的 ImageView 成比例,或者为您的 ImageView 添加一个 scaleType="fitCenter"。

【讨论】:

  • 是的,我应该更改任何图像尺寸吗?顺便说一句,我已经更新了我的帖子,也许我的 api 版本有问题?
  • @earlcabanig 从您的 ImageView 中删除 android:layout_alignParentLeft="true" android:layout_alignParentStart="true"。它覆盖了 centerHorizo​​ntal 行为。
  • 这是答案,我投了赞成票,但由于我的声誉低于 15,我似乎可以公开看到分数。顺便感谢 ^^
【解决方案2】:

试试下面的 xml 这对我有用..

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="20dp"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="200dp"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1"
            android:scaleType="fitCenter"
            android:src="@drawable/school" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="20dp"
            android:layout_weight="1">

            <EditText
                android:id="@+id/etUsername"
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:hint="Enter Sr-Code" />

            <EditText
                android:id="@+id/etPassword"
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:layout_below="@+id/etUsername"
                android:layout_centerHorizontal="true"
                android:hint="Password"
                android:inputType="textPassword" />

            <Button
                android:id="@+id/bLogin"
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/etPassword"
                android:layout_alignStart="@+id/etPassword"
                android:layout_below="@+id/etPassword"
                android:text="Log In" />

            <TextView
                android:id="@+id/guest"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/bLogin"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="20dp"
                android:text="Log In as Guest"
                android:textAppearance="?android:attr/textAppearanceSmall"/>

            <TextView
                android:id="@+id/testinguser"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/guest"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="20dp"
                android:text="Log In as Guest"
                android:textAppearance="?android:attr/textAppearanceSmall"/>

        </RelativeLayout>
    </LinearLayout>
</ScrollView>

【讨论】:

  • 先生,我尝试了您的代码并收到此错误“ java.lang.RuntimeException: Unable to start activity ComponentInfo{com.capstone.jmilibraryapp/com.capstone.jmilibraryapp.Login}: java.lang. ClassCastException: android.support.v7.widget.AppCompatImageView 不能在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 上转换为 android.widget.EditText" 也许我对 appcompactview 有问题?
  • 尝试使用旧版本的支持库 7 ur
猜你喜欢
  • 2014-10-18
  • 2011-11-06
  • 1970-01-01
  • 2012-04-17
  • 1970-01-01
  • 2018-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多