我試影像這樣在兩個視圖(水平和垂直)之間設定一個空間
<?xml version="1.0" encoding="utf-8"?>
<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=".Fragments.SetProfileFragment">
<TextView
android:id="@ id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginTop="4dp"
android:text="Test 1"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@ id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="4dp"
android:text="Test 2"
android:textSize="20sp"
app:layout_constraintStart_toEndOf="@id/textView1"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
但我希望兩者之間的空間TextViews為螢屏的 1%。是否可以?
uj5u.com熱心網友回復:
但我想將兩個 TextView 間隔 1% 的螢屏百分比。是否可以?
將開始和頂部約束設定為父級:使用具有根百分比的水平和垂直指南
ConstraintLayout:1% 被認為是 0.01,因為 1% 等于全屏寬度 >> 所以,使用
app:layout_constraintGuide_percent="0.01"您可以將約束應用于
textview1,并且為了不重復您自己,您可以將約束textView2應用于 的 BaseLinetextview1。用于在 TextViews 之間設定螢屏寬度的 1%:使用空間約束到
textView1螢屏寬度的 1% 的百分比:app:layout_constraintWidth_percent="0.01"。然后將 約束textView2到 Space,而不是textView。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments.SetProfileFragment">
<androidx.constraintlayout.widget.Guideline
android:id="@ id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.01" />
<androidx.constraintlayout.widget.Guideline
android:id="@ id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.01" />
<TextView
android:id="@ id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test 1"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="@id/guideline"
app:layout_constraintTop_toTopOf="@id/guideline2" />
<Space
android:id="@ id/space"
android:layout_width="0dp"
android:layout_height="match_parent"
app:layout_constraintStart_toEndOf="@id/textView1"
app:layout_constraintWidth_percent="0.01" />
<TextView
android:id="@ id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test 2"
android:textSize="20sp"
app:layout_constraintBaseline_toBaselineOf="@id/textView1"
app:layout_constraintStart_toEndOf="@id/space"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
uj5u.com熱心網友回復:
你可以試試這個代碼
val displayMetrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(displayMetrics)
val width = displayMetrics.widthPixels
val height = displayMetrics.heightPixels
val widthPer = (1*100)/width //1% percentage width
val heightPer = (1*100)/height //1% percentage height
textView1.setPadding(widthPer, heightPer, widthPer, heightPer) //Adding padding
textView2.setPadding(widthPer, heightPer, widthPer, heightPer) //Adding padding
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/326275.html
標籤:安卓 安卓布局 android-constraintlayout
上一篇:如何在androidx抽屜布局小部件中更改字體大小和顏色
下一篇:如何反轉XML形狀的邊框?
