如何在Android中使用RelativeLayout动态创建用户界面
我想通过代码动态创建一个相对布局,其中一个下面有两个文本视图。如何通过android中的代码实现android:Layout_below属性。 有人能帮我解决这个问题吗如何在Android中使用RelativeLayout动态创建用户界面,android,Android,我想通过代码动态创建一个相对布局,其中一个下面有两个文本视图。如何通过android中的代码实现android:Layout_below属性。 有人能帮我解决这个问题吗 提前感谢,这是我针对特殊问题的解决方案 final TextView upperTxt = (...) upperTxt.setId(12345); final TextView lowerTxt = (...); final RelativeLayout.LayoutParams params = RelativeLayou
提前感谢,这是我针对特殊问题的解决方案
final TextView upperTxt = (...)
upperTxt.setId(12345);
final TextView lowerTxt = (...);
final RelativeLayout.LayoutParams params = RelativeLayout.LayoutParams(this, null);
params.addRule(RelativeLayout.BELOW, 12345);
lowerTxt.setLayoutParams(params);
如果在数据库中找不到用户名,我必须创建一个类似于xml生成的RelativeLayout
// text view appears on top of the edit text
enterNameRequest = new TextView(mainActivity.getApplicationContext());
// fill the view with a string from strings.xml
enterNameRequest.setText(mainActivity.getResources().getString(R.string.enterNameRequest));
// edit text appears below text view and above button
enterName = new EditText(mainActivity.getApplicationContext());
enterName.setId(667);
// button appears at the bottom of the relative layout
saveUserName = new Button(mainActivity.getApplicationContext());
saveUserName.setText(mainActivity.getResources().getString(R.string.useUserName));
saveUserName.setId(666);
// generate the relative layout
RelativeLayout layout = new RelativeLayout(mainActivity.getApplicationContext());
layout.setId(668);
// set a background graphic by its id
layout.setBackgroundDrawable(mainActivity.getApplicationContext().getResources().getDrawable(R.drawable.background_head_neutral));
// runtime told me that i MUST use width and height parameters!
LayoutParams params2 = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.ABOVE, 666);
enterName.setLayoutParams(params2);
LayoutParams params3 = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params3.addRule(RelativeLayout.ABOVE, 667);
enterNameRequest.setLayoutParams(params3);
LayoutParams params4 = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params4.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 668);
saveUserName.setLayoutParams(params4);
// add views
layout.addView(enterNameRequest);
layout.addView(enterName);
layout.addView(saveUserName);
/* todo: set button action */
mainActivity.setContentView(layout);
我还发现:
从java内部手动操作布局不是很好
你最好使用一个新的活动并在其中设置一个新的布局
这样,应用程序代码的可读性更好
我甚至尝试在一个活动中设置几个布局(不是手动设置,而是使用setContentView),结果发现我不知道在哪里访问其他内容。。。另外,我在添加onClickListeners时遇到了一个很大的问题。。。因此,您最好在xml中的按钮标记中使用--android:onClick=“myButtonMethod”-,并在相应的活动中使用一个方法,该方法使用布局,如下所示:
public void myButtonMethod(View v){
// do stuff
}
这会提高性能,因为您没有使用其他侦听器,但在任何情况下都会使用绑定到活动的已可用侦听器。u可以尝试此方法
LinearLayout.LayoutParams leftMarginParams=新的LinearLayout.LayoutParams(
LayoutParams.WRAP_内容,LayoutParams.WRAP_内容)``
leftMarginParams.leftMargin=50
Button btn1 = new Button(this);
btn1.setText("Button1");
linLayout.addView(btn1, leftMarginParams)