changing place of button using layout function in absolute layout
I am trying to position a button in a different place using absolute layout. I am using the following xml file:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/bcgrd">
<Button
android:id="@+id/start_challenge"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "Start Challenge"
android:textColor= "@color/light_gray"
android:background="@color/background">
</Button>
</AbsoluteLayout>
the java file contains the following code:
Button start_it = (Button)findViewById(R.id.start_challenge);
start_it.layout(200, 200, 200, 200);
but nothing happen (the '200's are just for the example. can anyone please tell me what I am doing wro开发者_开发技巧ng.
Thanks in advance.
What you're doing wrong is using AbsoluteLayout. :) Seriously, you really shouldn't use it; try using a FrameLayout instead.
However, to answer your question, you shouldn't call layout on it, do this instead:
AbsoluteLayout.LayoutParams params = (AbsoluteLayout.LayoutParams)start_it.getLayoutParams();
params.x = 200;
params.y = 200;
start_it.setLayoutParams(params);
Or with less code, you could do this instead:
start_it.setLayoutParams(new AbsoluteLayout.LayoutParams(
start_it.getWidth(), start_it.getHeight(), 200, 200));
精彩评论