Android - 在代码中应用 9 补丁
如何在代码中将 9 补丁应用到 Android TextView?
你可能会问为什么?因为我需要动态设置控件的数量。
我尝试做类似的事情,但它不起作用:(
代码中我的9补丁“notif_bubble_white.9.png”是R.drawable.notif_bubble_white)
tv = new TextView(this);
tv.setText("Test Message");
tv.setLayoutParams(lpL);
tv.setPadding(0, 0, paddingValue, 0);
CustomFonts.ApplyFont_Custom1(tv, context);
tv.setBackgroundResource(R.drawable.notif_bubble_white);
// tv.setTextColor(TxtColFromCl);
layout.addView(tv);
其中lpL定义为:
LinearLayout.LayoutParams lpL = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
How may I apply a 9 patch to an Android TextView IN CODE?
Why, you may ask? Because I need to dynamically set the number of controls.
I'm try to do something similar and its not working:
(my 9 patch "notif_bubble_white.9.png" in code is R.drawable.notif_bubble_white)
tv = new TextView(this);
tv.setText("Test Message");
tv.setLayoutParams(lpL);
tv.setPadding(0, 0, paddingValue, 0);
CustomFonts.ApplyFont_Custom1(tv, context);
tv.setBackgroundResource(R.drawable.notif_bubble_white);
// tv.setTextColor(TxtColFromCl);
layout.addView(tv);
where lpL was defined as:
LinearLayout.LayoutParams lpL = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以一次性创建所有控件,并使用
control.setVisibility(View.GONE)
隐藏它们直到需要为止,或者,如果控件的数量未知,您可以膨胀布局文件,例如此问题中进行了描述。从引用的问题复制的代码(归功于 Cedric、或 Pentium10、idk,并假设控件是一个按钮):
button.xml:
膨胀按钮:
You can either just create the controls all at once and hide them with
control.setVisibility(View.GONE)
until needed, or, if the number of controls are unknown, you can inflate a layout file, like described in this question.Code copied from referenced question (credits to Cedric, or Pentium10, idk, and assuming control is a button):
button.xml:
Inflating the button: