简体   繁体   English

Android:编辑以编程方式添加的视图

[英]Android: Edit views added programmatically

So I have a messaging app where I read my database and render all the messages programatically.所以我有一个消息应用程序,我可以在其中读取我的数据库并以编程方式呈现所有消息。 The thing is, when I am inside a different layout, I need to inflate those messages and edit them at will.问题是,当我处于不同的布局中时,我需要扩充这些消息并随意编辑它们。 For example,例如,

val myView = inflater.inflate(R.layout.female_messaging_fragment, container, false)
...

val convLayout = myView.findViewById<LinearLayout>(R.id.conversations_layout)
val profileLayout = LinearLayout(context)

 val recentMessage = TextView(context)
 recentMessage.id = 5



val recentParams = LinearLayout.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT)

            recentParams.setMargins(10, 0, 0, 0)

            recentMessage.layoutParams = recentParams
            recentMessage.textSize = 16f
            if (didIsendLastMessage) {
                val arrowIcon = ImageView(context)
                Picasso.get().load(R.drawable.right_arrow).resize(50, 50).into(arrowIcon)
                val imageParams = LinearLayout.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT)
                imageParams.gravity = Gravity.CENTER
                arrowIcon.layoutParams = imageParams
                recentMessage.setText(mostRecentMessage)
                arrowAndMessageLayout.addView(arrowIcon)
                arrowAndMessageLayout.addView(recentMessage)
            } else {
                recentMessage.setText(mostRecentMessage)
                arrowAndMessageLayout.addView(recentMessage)
            }


            nameLastmessageLayout.addView(arrowAndMessageLayout)


            // Add namne and message to profile
            profileLayout.addView(nameLastmessageLayout)
convLayout.addView(profileLayout)

And then in a different layout...然后在不同的布局...

LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.female_messaging_fragment, null);
TextView text = (TextView) v.findViewById(5);
text.setText("Something");

The thing is, text is always null, and I believe it is because I added the views programatically.问题是,文本始终为空,我相信这是因为我以编程方式添加了视图。 What else can I do here?我还能在这里做什么? Thanks!谢谢!

inflating female_messaging_fragment will return fresh new Layout view object of your xml that might be blank layout without any TextViews. female_messaging_fragment将返回您的 xml 的全新布局视图对象,它可能是没有任何 TextViews 的空白布局。

To get TextView object you need the object of Layout in which you had added the views.要获取 TextView 对象,您需要在其中添加视图的 Layout 对象。

For example as per your code you added recentMessage in arrowAndMessageLayout so to get the object of recentMessage you need the object of arrowAndMessageLayout and can get like below -例如,根据您的代码,您在arrowAndMessageLayout添加了recentMessage ,因此要获取recentMessage的对象,您需要使用arrowAndMessageLayout的对象,并且可以获得如下所示的内容 -

TextView text = arrowAndMessageLayout.findViewById(5);

OR或者

TextView text = convLayout.findViewById(5);

Inflating again will return fresh new container without your textview.再次充气将返回没有你的 textview 的新容器。

Hope this will help you.希望这会帮助你。

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

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