And*_*Jay 8 java android android-layout android-relativelayout
我很难在我的java代码中让两个文本视图相互叠加.这是我正在尝试的代码:
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
layout = new RelativeLayout(this);
text1 = new TextView(this);
text1.setText("1");
text2 = new TextView(this);
text2.setText("2");
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams q = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
q.addRule(RelativeLayout.BELOW, layout.getId());
text1.setLayoutParams(q);
layout.addView(text1);
p.addRule(RelativeLayout.BELOW,text1.getId());
text2.setLayoutParams(p);
layout.addView(text2);
setContentView(layout);
}
Run Code Online (Sandbox Code Playgroud)
这将两个文本视图堆叠在同一行,但我希望TextView text2出现在TextView text1下面,所以在我的应用程序中,我希望以下内容显示为输出:
1
2
Run Code Online (Sandbox Code Playgroud)
我用"addRule"方法尝试了各种各样的东西,我不知道为什么这不起作用.我想知道如何在没有XML的情况下执行此操作,因为我计划构建一个方法库,可以构建一个可以通过编辑数组轻松调整的布局.
你TextViews没有id(默认情况下id是-1)...在初始化之后把它放进去:
text1.setId(1111); // 1111 is just an example,
text2.setId(2222); // just make sure the id are unique
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15791 次 |
| 最近记录: |