[英]Android RelativeLayout align center of one view on top right corner of another view
我有使用 RelativeLayout 的經驗,但我從來沒有遇到過解決我遇到的問題的方法(除了我想避免的硬編碼邊距值。)
我想嘗試在 RelativeLayout 中創建類似於下圖的內容:
該框是它自己的視圖,我想讓包含橙色圓圈的視圖以包含藍色框的視圖的右上角為中心。
我試過android:alignTop="boxView"
和android:alignRight="boxView"
但這把我的橙色圓圈完全放在我的盒子里。 我希望它使圓圈位於框右上角上方的中心。
有誰知道我怎樣才能用RelativeLayout 得到那個結果? 最好不必為橙色點視圖硬編碼遠離屏幕邊緣的邊距。
此代碼創建了您正在尋找的內容,但確實使用了邊距。 如果這是您正在創建的動態結構,現在您可以在代碼中設置邊距。 如您所見,我使用負邊距將右上角的形狀移動到藍色框外。 這些需要是您嘗試移動的圓圈高度的一半。 您可以在代碼中完成所有這些操作,以將圓圈置於右上角。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#0000FF"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="26dp"
android:layout_height="26dp"
android:layout_alignRight="@+id/linearLayout1"
android:layout_alignTop="@+id/linearLayout1"
android:layout_marginRight="-13dp"
android:layout_marginTop="-13dp"
android:background="#FF00FF"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
您可以使用 ConstraintLayout 圓形定位,只需將您的視圖對齊 45 度角並調整半徑:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:background="@color/colorAccent"
android:id="@+id/view"
android:layout_width="100dp"
android:layout_height="100dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:background="@color/colorPrimary"
android:id="@+id/alignedView"
android:layout_width="30dp"
android:layout_height="30dp"
app:layout_constraintCircle="@id/view"
app:layout_constraintCircleAngle="45"
app:layout_constraintCircleRadius="60dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
最好和正確的做法:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_10sdp">
<View
android:id="@+id/viewBox"
android:layout_width="35dp"
android:layout_height="35dp"
android:background="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent" />
<View
android:layout_width="15dp"
android:layout_height="15dp"
android:src="@drawable/ic_close"
app:layout_constraintBottom_toTopOf="@+id/viewBox"
app:layout_constraintLeft_toRightOf="@+id/viewBox"
app:layout_constraintRight_toRightOf="@+id/viewBox"
app:layout_constraintTop_toTopOf="@+id/viewBox" />
</androidx.constraintlayout.widget.ConstraintLayout>
使用約束布局很容易實現。
只需將圓的開始和結束約束設置為相同的點,將頂部和底部設置為相同的點(例如 textview 的右上角),如下所示:
<TextView
android:id="@+id/newWarningLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left|center"
android:lines="2"
android:text="testtest"
android:textColor="@color/white"
android:textSize="12sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="@+id/alertCounter"
android:layout_width="20dp"
app:layout_constraintStart_toStartOf="@id/newWarningLabel"
app:layout_constraintEnd_toStartOf="@id/newWarningLabel"
app:layout_constraintTop_toTopOf="@id/newWarningLabel"
app:layout_constraintBottom_toTopOf="@id/newWarningLabel"
android:layout_height="20dp"
android:gravity="center"
android:textColor="@color/white"
android:textStyle="bold"
android:textSize="10sp"
android:visibility="visible"
android:text="1"
android:background="@drawable/red_circle"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.