简体   繁体   English

android 以编程方式设置保证金

[英]android set margin programmatically

after i press a button, i would like to move a textfield programmatically from the actual position + 20 margin left.按下按钮后,我想以编程方式从实际的 position + 20 左边距移动一个文本字段。

this is my xml file:这是我的 xml 文件:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="5dp"
    android:paddingBottom="5dp">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txtField"
        android:layout_below="@+id/txtField2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="20dp"
        android:layout_toLeftOf="@+id/Seperator"
        android:layout_toStartOf="@+id/Seperator"
        android:layout_marginRight="10p" />

    ....


</RelativeLayout>

i tried this code:我试过这段代码:

parameter = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
parameter.setMargins(30, 32, 10, 0); // left, top, right, bottom
txtField.setLayoutParams(parameter);

this works semi optimal.这工作半最佳。 is there an way to use all the values of an xml file and only change the margin left value programmatically?有没有办法使用 xml 文件的所有值,并且只以编程方式更改边距左边值?

Try this code:试试这个代码:

parameter =  (RelativeLayout.LayoutParams) txtField.getLayoutParams();
parameter.setMargins(leftMargin, parameter.topMargin, parameter.rightMargin, parameter.bottomMargin); // left, top, right, bottom
txtField.setLayoutParams(parameter);

Yep,是的,

Take for example the following method that updates the left margin of a given view:以更新给定视图的左边距的以下方法为例:

public static void setMarginLeft(View v, int left) {
    ViewGroup.MarginLayoutParams params =   
                                (ViewGroup.MarginLayoutParams)v.getLayoutParams();
    params.setMargins(left, params.topMargin, 
                            params.rightMargin, params.bottomMargin);
}

If you use kotlin, this can be simplified by creating an extension function如果你使用 kotlin,这可以通过创建一个扩展函数来简化

fun View.setMarginLeft(leftMargin: Int) {
  val params = layoutParams as ViewGroup.MarginLayoutParams
  params.setMargins(left, params.topMargin, params.rightMargin, params.bottomMargin)
  layoutParams = params
}

Now all you need is a view, and this extension function can be used anywhere.现在你只需要一个视图,这个扩展功能可以在任何地方使用。

val textView = findViewById(R.id.textView)
val marginLeft = context.resources.getDimension(R.dimen.marginLeft).toInt()
textView.setMarginLeft(marginLeft)

Note: This extension function will leave all other margins intact, no margins will be overwritten注意:此扩展功能将保留所有其他边距不变,不会覆盖任何边距

You can also try你也可以试试

ImageView _circle=(ImageView) findViewById(R.id.image);
ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) _circle.getLayoutParams();
int _20 = (int)getResources().getDimension(R.dimen.image_margin); 
// Directly set 20
mlp.setMargins(_20, _20, _20, _20);

Extending @enyciaa answer: by using Kotlin you can use extensions, but instead create one extension function for each margin side, you can use nullable value for all sides and then fill only values that's not null:扩展@enyciaa 答案:通过使用 Kotlin,您可以使用扩展,而是为每一边创建一个扩展函数,您可以为所有边使用可为空的值,然后只填充不为空的值:

fun View.setMargin(
    leftMargin: Int? = null,
    topMargin: Int? = null,
    rightMargin: Int? = null,
    bottomMargin: Int? = null
) {
    val params = layoutParams as ViewGroup.MarginLayoutParams

    params.setMargins(
        leftMargin ?: params.leftMargin,
        topMargin ?: params.topMargin,
        rightMargin ?: params.rightMargin,
        bottomMargin ?: params.bottomMargin
    )
    
    layoutParams = params
}

So if you want to set only left margin, you can do it by specifying parameter name like this:所以,如果你只想设置左边距,您可以通过指定这样的参数名称做到这一点:

textView.setMargin(leftMargin = marginValue)

This will work in Android 30这将适用于 Android 30

val textView = TextView(this)
val layoutParams = ViewGroup.MarginLayoutParams(10, 10)
layoutParams.marginStart = 5
textView.layoutParams = layoutParams

To set padding -设置填充 -

textView.setPadding(2, 2, 2, 2)

Just use these few lines of code in kotlin就用kotlin这几行代码

val params = your_view.layoutParams as ViewGroup.MarginLayoutParams
params.setMargins(dpToPx(80, this), dpToPx(0, this), dpToPx(0, this), dpToPx(0, this))

Here is the code for the function dpToPx这是 function dpToPx 的代码

private fun dpToPx(dp: Int, context: Context): Int {
    val density = context.resources.displayMetrics.density
    return (dp.toFloat() * density).roundToInt()
}

If you are using fragment, use requireContext() as parameter for context.如果您使用的是片段,请使用requireContext()作为上下文的参数。 From activity, use this as context like I have done从活动中,像我一样使用this作为上下文

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM