我是android的新手,我正在开发一个应用程序,我想动态设置textview的边框,即代码而不是xml。我在谷歌搜索了很多但是到处都找到了xml解决方案。我尝试了很多,但没有找到正确的方法来实现它。 喜欢使用这个网址,但我无法得到结果 How to draw borders for TextView created using Code not by XML in Android 请建议我的想法,我怎么能实现这个...
答案 0 :(得分:2)
您不会找到关于非XML布局的大量文档的原因是因为大多数问题都可以通过布局来解决。我建议至少在XML中定义textview,然后在代码中设置边框。例如:
在layout.xml文件中:
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a TextView" />
然后在您的代码中:
TextView text = ((TextView)this.findViewById(R.id.text)); //use id to find view
ViewGroup.LayoutParams textLayout = text.getLayoutParams();
textLayout.topMargin = 10;
textLayout.bottomMargin = 10;
text.setLayoutParams(textLayout);
您可以通过这种方式修改对象的任何属性。 (使用ViewGroup和View作为资源)
注意上面的例子只是示例(我在前面没有编译器来检查语法和效果)