开发者

How to align a dynamically create UserInterface using RelativeLayout in Android

I want to create a 开发者_开发百科relative Layout dynamically through code with 2 Textviews one below the other.How to implement android:layout_below property through code in Android. can anyone help me in sorting out this issue.

Thanks in Advance,


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


Here is my solution for my special Problem.

In case the username wouldn't be found in the db i had to create a RelativeLayout that looks like the xml-generated one.

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

What i found out additionally: It is not so good to manipulate the layout manually from within java!

You should better use a new Activity and set a new layout in it.

This way, the application-code is readable a lot better!

I even tried to set several layouts (not manually, but wit setContentView) in one activity, and it turned out that i didn't know where what was accessing what else... Also, i had a great problem in adding onClickListeners... so you better use -- android:onClick="myButtonMethod" -- in your button tag in the xml and have a method in your according activity, which uses the layout, like this:

public void myButtonMethod(View v){
     // do stuff
}

This improves performance because you are not using additional Listeners - but you use the already available Listener that is bound to your activity in every case.


u can try this

LinearLayout.LayoutParams leftMarginParams = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);`` leftMarginParams.leftMargin = 50;

    Button btn1 = new Button(this);
    btn1.setText("Button1");
    linLayout.addView(btn1, leftMarginParams)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜