【问题标题】:How to align view by center point?如何按中心点对齐视图?
【发布时间】:2016-06-14 14:01:20
【问题描述】:

我想创建这个布局

然后我发现了这个

Android - How do I position views in an offset relative to the center/top/bottom (etc.) of their parent?

所以我创建了这个布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="com.myapplication.MainActivity">
    <View
        android:id="@+id/square"
        android:layout_width="100dp"
        android:background="#ff0000"
        android:layout_centerInParent="true"
        android:layout_height="100dp"/>
    <ImageView
        android:id="@+id/circle"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignTop="@id/square"
        android:layout_alignLeft="@id/square"
        android:layout_marginRight="25dp"
        android:layout_marginBottom="25dp"
        android:src="@mipmap/circle" />
</RelativeLayout>

但是布局边距没有任何作用

输出与

相同
<ImageView
    android:id="@+id/circle"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_alignTop="@id/square"
    android:layout_alignLeft="@id/square"
    android:src="@mipmap/circle" />

如何将视图的中心点与其他视图的某个点对齐?

【问题讨论】:

  • 在您的代码中处于水平和垂直工作的中心吗?试试看
  • 将圆圈放在正方形内,然后向上移动并离开整个半径

标签: android android-layout


【解决方案1】:

你好,我做了一个例子,对你有帮助

请检查以下代码和输出

<FrameLayout
                android:id="@+id/FrameMove"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="8dp">

                <LinearLayout
                    android:id="@+id/linearFramelayout"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="5dp"
                    android:layout_marginTop="10dp"
                    android:background="@drawable/frame_drawable"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:paddingBottom="-50dp">

                    <TextView
                        android:id="@+id/txtMove"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@android:color/white"
                        android:textSize="20sp"
                        android:textStyle="bold"/>
                </LinearLayout>


                <ImageView
                    android:id="@+id/imgClose"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="end|top"
                    android:layout_marginRight="-8dp"
                    android:layout_marginTop="-8dp"
                    android:src="@drawable/ic_action_close_white"/>

            </FrameLayout>

【讨论】:

  • 这个答案在我目前的布局中很难应用,但最后我从这个答案中得到启发,我只需要设置android:layout_marginRight="-25dp",然后问题就解决了,所以这是仍然是一个有用的答案
【解决方案2】:

一个选项是给正方形一个边距顶部和边距左,等于圆的半径。让两个元素都有属性alignParentLeft=true

【讨论】:

    【解决方案3】:

    如果您知道圆形图像的宽度和高度,那么这很容易 你需要使用一个框架布局,并给正方形的左边距和上边距一个圆的宽度和高度的一半

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-06
      • 2021-09-20
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      相关资源
      最近更新 更多