使用Kotlin将运行时边距设置为任何视图

Chi*_*nki 9 android button kotlin

我是Kotlin的一员.我对这种语言并不太熟悉.我正在做一个例子并且玩代码.我只是想将运行时余量设置为任何视图.我也试图谷歌它但没有得到任何适当的解决方案这个任务.更多代码详情请阅读以下内容.

需求

将运行时边距设置为任何视图.

描述

我有一个包含在Button上的xml文件,我想将运行时余量设置为此按钮.

我也尝试下面的东西,但它不起作用.

class MainActivity : AppCompatActivity() {


//private lateinit var btnClickMe: Button
//var btnClickMe=Button();

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //For setting runtime text to any view.
        btnClickMe.text = "Chirag"

        //For getting runtime text to any view
        var str: String = btnClickMe.text as String;

        //For setting runtimer drawable
       btnClickMe.background=ContextCompat.getDrawable(this,R.drawable.abc_ab_share_pack_mtrl_alpha)//this.getDrawable(R.drawable.abc_ab_share_pack_mtrl_alpha)


        /*
        //For Setting Runtime Margine to any view.
        var param:GridLayout.LayoutParams
        param.setMargins(10,10,10,10);

        btnClickMe.left=10;
        btnClickMe.right=10;
        btnClickMe.top=10;
        btnClickMe.bottom=10;
        */

        // Set OnClick Listener.
        btnClickMe.setOnClickListener {
            Toast.makeText(this,str,5000).show();
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

}

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
tools:context="chirag.iblazing.com.stackoverflowapp.MainActivity"
android:layout_height="match_parent">

<Button
    android:id="@+id/btnClickMe"
    android:text="Click Me"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我希望你们都明白我的问题.如果有人解决这个问题,请帮助我.如果你想了解更多关于我的问题的信息,请告诉我.

提前致谢.

cha*_*l03 26

你需要得到layoutParams来自按钮对象,并将其转换为LinearLayout.LayoutParamsbtnClickMe的父LinearLayout,并设置页边距,以任何你想要的.

检查以下代码:

val param = btnClickMe.layoutParams as LinearLayout.LayoutParams
param.setMargins(10,10,10,10)
btnClickMe.layoutParams = param // Tested!! - You need this line for the params to be applied.
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.


Hit*_*pta 15

当我需要设置运行时边距时,我就是在Kotlin中完成的 -

fun View.margin(left: Float? = null, top: Float? = null, right: Float? = null, bottom: Float? = null) {
    layoutParams<ViewGroup.MarginLayoutParams> {
        left?.run { leftMargin = dpToPx(this) }
        top?.run { topMargin = dpToPx(this) }
        right?.run { rightMargin = dpToPx(this) }
        bottom?.run { bottomMargin = dpToPx(this) }
    }
}

inline fun <reified T : ViewGroup.LayoutParams> View.layoutParams(block: T.() -> Unit) {
    if (layoutParams is T) block(layoutParams as T)
}

fun View.dpToPx(dp: Float): Int = context.dpToPx(dp)
fun Context.dpToPx(dp: Float): Int = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resources.displayMetrics).toInt()
Run Code Online (Sandbox Code Playgroud)

现在我们只需要在类似的视图上调用它

textView.margin(left = 16F)
Run Code Online (Sandbox Code Playgroud)


P K*_*ers 9

这是一个有用的 Kotlin 扩展方法:

fun View.setMargins(
    left: Int = this.marginLeft,
    top: Int = this.marginTop,
    right: Int = this.marginRight,
    bottom: Int = this.marginBottom,
) {
    layoutParams = (layoutParams as ViewGroup.MarginLayoutParams).apply {
        setMargins(left, top, right, bottom)
    }
}
Run Code Online (Sandbox Code Playgroud)

像这样使用它:

myView.setMargins(
   top = someOtherView.height
   bottom = anotherView.height
)
Run Code Online (Sandbox Code Playgroud)

编辑:解决方案类似于Hitesh 的答案,但我使用的是(原始)ViewGroup.setMargins像素。当然,您可以setMarginsDp根据这些示例制作自己的变体,或者dpToPx在调用我的实现之前使用 Hitesh 的扩展。您选择哪种解决方案取决于您自己的口味。

另请注意,我的解决方案(重新)设置了所有边距,尽管在大多数情况下这不会成为问题。