如何在Kotlin中创建View并将TextView放入其中

时间:2018-12-13 08:19:59

标签: android kotlin

我想通过代码在Kotlin的View对象内部添加TextView

我确实创建了View,但是无法在其中添加Text View

val view1 : View
  view1 = View(this)
  myLayout.addView(view1)
  view1.layoutParams.height = (Height * 0.35).toInt()
  view1.layoutParams.width = (Width * 0.8).toInt()
  view1.x = (1+ Width*0.10).toFloat()
  view1.y = (Height*0.15).toFloat()
  view1.setBackgroundColor(Color.rgb(128,0,128))

2 个答案:

答案 0 :(得分:0)

val view1 : TextView = TextView(this)
myLayout.addView(view1)
view1.layoutParams.height = (Height * 0.35).toInt()
view1.layoutParams.width = (Width * 0.8).toInt()
view1.x = (1+ Width*0.10).toFloat()
view1.y = (Height*0.15).toFloat()
view1.text = "Some text"
view1.setBackgroundColor(Color.rgb(128,0,128))

答案 1 :(得分:0)

您的父视图必须是ViewGroup。例如:

<?php
$allowed_char_array=array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","à","á","â","ã","ä","å","æ","ç","è","é","ê","ë","ì","í","î","ï","ñ","ò","ó","ô","õ","ö","ð","ø","œ","š","Þ","ù","ú","û","ü","ý","ÿ","ž","0","1","2","3","4","5","6","7","8","9"," ","(",")","-","_",".","@","#","$","%","*","¢","ß","¥","£","™","©","®","ª","×","÷","±","+","-","²","³","¼","½","¾","µ","¿","¶","·","¸","º","°","¯","§","…","¤","¦","≠","¬","ˆ","¨","‰");
$word = 'imageЙ ййé.png';
$file_name = url_rewrite(trim($word));
$file_name2 = strtolower($file_name);
$split = str_split($file_name2);

if(is_array($split) && is_array($allowed_char_array)){
	$result=array_diff($split,$allowed_char_array);
	echo '<pre>';
	print_r($split);
	echo '<pre>';
	print_r($allowed_char_array);
	echo '<pre>';
	print_r($result);
}
function url_rewrite($chaine) {

    // On va formater la chaine de caractère
    // On remplace pour ne plus avoir d'accents
    $accents = array('é','à','è','À','É','È');
    $sans =    array('é','à','è','À','É','È');
    $chaine = str_replace($accents, $sans, $chaine);

    
    return $chaine;
}
?>