kotlin中的相对布局参数

时间:2017-12-06 11:55:52

标签: android kotlin relativelayout

我正在尝试以编程方式设置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)


    }
}

感谢任何帮助!

4 个答案:

答案 0 :(得分:2)

它对我有用::

    var  param: RelativeLayout.LayoutParams = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,100);
    param.setMargins(500,12,0,0)
    val i:Int=1
    var temp : TextView = TextView(this);
    temp.id=i
    temp.layoutParams = param ;
    temp.text= "TextView\nadfadsfasdfadsfasdf\nadfadsfasdfa\nadfadsfasdf"+i
    Log.e(ContentValues.TAG, "onCreate: " + i)

请参阅此处生成的输出:Output of Above code

谢谢,如果您需要更多,请告诉我。

答案 1 :(得分:1)

    lateinit var r1: RelativeLayout
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
       r1= RelativeLayout(this)
       var rlp : RelativeLayout.LayoutParams = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT)

    }

答案 2 :(得分:0)

var view: RelativeLayout =RelativeLayout(this)
view.layoutParams= RelativeLayout.LayoutParams(25,25)

试试这个...

答案 3 :(得分:0)

这对我有用

var tempSnack = Snackbar.make(view,"Your Text Here", Snackbar.LENGTH_LONG) var tempview = tempSnack.view var params = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT) params.gravity = Gravity.TOP tempview.layoutParams = params tempSnack.show()