【问题标题】:How to lay image over another image where the middle of the image should align with the bottom of the image underneath如何将图像放在另一个图像上,其中图像的中间应与下方图像的底部对齐
【发布时间】:2012-08-21 06:16:20
【问题描述】:

我在 Android 布局上显示了一个横幅。在这个横幅上,我有两个头像,我想将它们并排显示,最重要的是,我想让它们显示在这两个头像在 y 轴上的中点与这些头像所在的横幅的底部。

你会怎么做?

编辑:

换句话说,我问的是如何使用像 android:layout_below 这样的参数,而不是将 imageview 的顶部与指定布局的底部对齐,而是对齐中心。

【问题讨论】:

    标签: android imageview layoutparams


    【解决方案1】:

    不幸的是,没有直接的布局参数可以将中心点与另一条边对齐。如果您的头像的高度是固定的,您可以添加一些高度为一半的填充,以便它们全部对齐;即

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/banner"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="25dp"
            android:src="@drawable/banner" />
    
        <ImageView
            android:id="@+id/avatar1"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_alignBottom="@id/banner"
            android:src="@drawable/horse" /> 
        <ImageView
            android:id="@+id/avatar2"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_alignBottom="@id/banner"
            android:layout_toRightOf="@id/avatar1"
            android:src="@drawable/horse" />
    </RelativeLayout>
    

    但是,如果这些项目的高度是动态的,那么您将需要创建一个自定义 ViewGroup 容器,以便您可以测量头像高度(在 onMeasure() 中)并应用填充(或其他偏移值)在运行时。

    【讨论】:

      【解决方案2】:

      将它们放在线性布局中,然后给它们一个宽度以填充父级。然后你可以使用 weight 属性来平均分散宽度。

      <?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:orientation="horizontal" >
      
      
          <ImageView
              android:id="@+id/imageView1"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:src="@drawable/clock" />
      
          <ImageView
              android:id="@+id/imageView2"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:src="@drawable/clock" />
      
      </LinearLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-13
        • 2023-03-10
        • 2016-05-27
        相关资源
        最近更新 更多