0

我是 android 的新手,我正在开发一个应用程序,我想在其中动态设置 textview 的边框,即通过代码而不是通过 xml。我在谷歌上搜索了很多,但我到处都找到了 xml 的解决方案。我尝试了很多,但没有找到正确的方法来实现这一点。喜欢使用这个 url,但我无法得到结果 How to drawborders for TextView created using Code not by XML in Android 请建议我的想法,我怎么能做到这一点..

4

1 回答 1

2

您找不到太多关于非 XML 布局的文档的原因是大多数问题都可以通过布局解决。我建议至少在 XML 中定义文本视图,然后在代码中设置边框。例如:

在 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);

您可以通过这种方式修改对象的任何属性。(使用ViewGroupView作为资源)

注意上面的例子只是例子(我面前没有编译器来检查语法和有效性)

于 2012-10-14T18:01:57.793 回答