Android Studio,Kotlin,更改 Layout_MarginStart 并动态结束

Android Studio, Kotlin, Change Layout_MarginStart and End dynamically

我正在尝试通过单击按钮更改 android:layout_marginStart 和 android:layout_marginEnd 和 imageView,但我似乎只能做一个 px 而不是 dp 的需要。当 activity 加载时,我将它限制在另一个图像的左侧和右侧,另一个图像是 300dp 宽,我想移动的图像的边距设置为 150dp 和 150dp,我希望能够设置这个对于任何东西,目前正在测试 30dp(开始)和 270dp(结束),所以它将是从左边开始的 10%。

XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Result">

<ImageView
    android:layout_width="300dp"
    android:layout_height="50dp"
    android:id="@+id/bar"
    android:minHeight="50dp"
    android:minWidth="300dp"
    android:layout_marginEnd="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    android:layout_marginStart="8dp"
    app:layout_constraintStart_toStartOf="parent"
    android:layout_marginTop="16dp"
    app:layout_constraintTop_toTopOf="parent"
    android:backgroundTint="#00050505"/>

<ImageView
    android:layout_width="60dp"
    android:layout_height="60dp"
    app:srcCompat="@drawable/ic_arrow_drop_up_black_24dp"
    android:id="@+id/imageView4"
    app:layout_constraintStart_toStartOf="@+id/bar"
    app:layout_constraintEnd_toEndOf="@+id/bar"
    android:layout_marginStart="150dp"
    android:layout_marginEnd="150dp"
    app:layout_constraintTop_toTopOf="@+id/bar"
    app:layout_constraintBottom_toBottomOf="@+id/bar"
    android:layout_marginTop="40dp"/>

<Button
    android:text="Change"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button"
    android:layout_marginTop="8dp"
    app:layout_constraintTop_toBottomOf="@+id/bar"
    android:layout_marginBottom="8dp"
    android:onClick="changeValue" android:layout_marginStart="8dp"
    app:layout_constraintStart_toStartOf="parent"     
    android:layout_marginEnd="8dp"
    app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

Kotlin 文件:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.result)   
}

fun changeValue(view:View){
        imageView4.updateLayoutParams<ConstraintLayout.LayoutParams> {
        marginEnd = 270
        marginStart = 30
    }
}

像这样设置 marginEnd 和 marginStart 要求的是像素而不是 dp,因此所有手机都不一样,有没有办法将 150dp(开始)和 150dp(结束)更改为其他值?

谢谢

有两种解决方法:

  1. 将此边距添加到您应用的维度资源中。在你的 activity/fragment 中使用 getResources().getDimension(R.dimen.your_margin);得到它

您将在文件的 dp 中设置它,但是 getResources().getDimension(...) 将 return 您在 px

中计算值

参见:Load dimension value from res/values/dimension.xml from source code

  1. 您可以使用 Anko 库。它包含方法 dip(...)。例如 dip(150) 将 return 您的像素值为 150 dp。参见:https://github.com/Kotlin/anko

希望对您有所帮助

通过Peter的回复,我想通了,so easy,我觉得很傻,谢谢Peter!我从没想过要检查 .width,我只是假设它会在不检查的情况下以 dp 的形式给我。

val barInPixels = bar.width
val quarter = 0.25 * barInPixels
val threeQuarters = 0.75 * barInPixels