我是新Android开发,我目前享受它了。
I am new to Android development and am currently enjoying it a lot.
我一直在使用XML文件构建UI,并发现它很容易建立一个静态的UI。
I have been building a UI by using the XML file and found it quite easy to build a static UI.
现在,我需要建立一个UI运行期间TextView的位置变化,其中之一。
Now, I need to build a UI where one of the TextView positions changes during runtime.
可通过使用XML来完成而已?
Can this be done using XML only?
<?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: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>
现在我可以通过修改 layout_x
的价值和 layout_y
任意位置TextView的。但是,如何在运行时做到这一点?
Now I can position the TextView anywhere by changing the value of layout_x
and layout_y
. But how to do this at runtime?
我已经试过这样:
TextView text;
text=(TextView)findViewById(R.id.percent);
做 text.setText(这是可以做到);
会轻易改变在运行时的内容,但我怎么可以改变从code中的现在的位置?
Doing text.setText("This can be done");
would easily change the text during run time, but how can I change the postion from code?
如果使用绝对布局尝试这一点,看看下面code。
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
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);
}
});
}
}