Tag: relativelayout

相对布局参数在kotlin

我正尝试以编程方式设置relative layout params 。 我可以使用java设置params但不知道如何使用kotlin来完成。 所以我已经尝试了如下: MainActivity类 class MainActivity : AppCompatActivity() { lateinit var context : Context override fun onCreate(savedInstanceState: android.os.Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) context=this asdasd.text=”finaly done”; var param:RelativeLayout.LayoutParams =//don’t know what to do here val i:Int=1 var temp :TextView = TextView(this); temp.id=i temp.layoutParams = param ; temp.text= “TextView”+i Log.e(ContentValues.TAG, “onCreate: ” + i) } […]

如何解释这个怪异的行为,当以编程方式将边距设置为RelativeLayout中的视图?

我有一个在TabView中的三个选项卡之一的片段内的RelativeLayout。 看到这个截图 – 大粉红色方块是我的RelativeLayout: 截图 里面的视图是右下角的小蓝色方块。 由于RelativeLayout是300x300dp,小方块是8x8dp,所以如果我将它的顶部和左边距设置为292dp,它就会在那个角落结束。 现在我想通过编程来改变自己的位置,但是当我这样做的时候,我的价值观却不断地被两分为二。 所以,如果我将边距从292改为292,它将会在RelativeLayout的中间位置结束,但是如果我将它们设置为292 * 2,则会回到角落。 也许有人知道这里发生了什么? 这是我的布局文件: <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <android.support.constraint.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:width="40dp" android:height="40dp" android:background="@color/colorAccent" android:text="Test!" /> <RelativeLayout android:layout_width="300dp" android:layout_height="300dp" android:background="@color/colorAccent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent"> <View android:id="@+id/position_dot" android:layout_width="8dp" android:layout_height="8dp" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:background="@color/colorPrimary" android:layout_marginLeft="292dp" android:layout_marginTop="292dp"/> </RelativeLayout> </android.support.constraint.ConstraintLayout> </layout> 这是我用来更新边距的方法: fun moveDotToCurrentPosition() { var […]