XML attributes and their corresponding related methods
I am new to Android development and am currently enjoying it a lot.
I have been building a UI by using the XML file and found it quite easy to build a static UI.
Now, I need to build a UI where one of the TextView positions changes during runtime.
Can this be done using XML only?
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:andr开发者_如何学Pythonoid="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_height="wrap_content"
android:text="0%"
android:layout_width="wrap_content"
android:id="@+id/percent"
android:textSize="15sp"
android:layout_x="41dip"
android:layout_y="295dip">
</TextView>
</AbsoluteLayout>
Now I can position the TextView anywhere by changing the value of layout_x
and layout_y
. But how to do this at runtime?
I have tried this:
TextView text;
text=(TextView)findViewById(R.id.percent);
Doing text.setText("This can be done");
would easily change the text during run time, but how can I change the postion from code?
Firstly, absolute layout is deprecated, use relative layout instead, Frame-Layout.
Then use,
LayoutParams params = new LayoutParams( MarginLayoutParams.WRAP_CONTENT, MarginLayoutParams.WRAP_CONTENT);
If asked to import, use import selected layoutparams.
After that use
params.leftmargin
or
params.rightmargin
for changing the position.
If trying out this by using absolute layout, take a look at the following code.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:text="@string/hello"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_alignParentRight="true">
</TextView>
<EditText
android:hint="xcor"
android:layout_height="wrap_content"
android:id="@+id/xcor"
android:background="@drawable/select"
android:layout_x="165dip"
android:layout_width="wrap_content"
android:layout_y="64dip">
</EditText>
<EditText
android:hint="ycor"
android:layout_height="wrap_content"
android:id="@+id/ycor"
android:layout_x="168dip"
android:layout_width="wrap_content"
android:layout_y="168dip">
</EditText>
<Button
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="Set Position"
android:layout_x="218dip"
android:layout_width="wrap_content"
android:layout_y="278dip"
android:background="@drawable/select"
>
</Button>
</AbsoluteLayout>
CustomPosition.java
public class CustomPosition extends Activity {
/** Called when the activity is first created. */
LayoutParams params = new LayoutParams(MarginLayoutParams.WRAP_CONTENT, MarginLayoutParams.WRAP_CONTENT, 150, 50);
TextView text;
EditText xcor;
EditText ycor;
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text=(TextView)findViewById(R.id.textView1);
xcor=(EditText)findViewById(R.id.xcor);
ycor=(EditText)findViewById(R.id.ycor);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
params.x=Integer.parseInt(xcor.getText().toString());
params.y=Integer.parseInt(ycor.getText().toString());
text.setLayoutParams(params);
}
});
}
}
精彩评论