2011-03-22 47 views
1

我是Android開發新手,目前很喜歡它。XML屬性及其對應的相關方法

我一直在使用XML文件構建用戶界面,發現構建靜態用戶界面非常容易。

現在,我需要構建一個UI,其中一個TextView位置在運行時發生更改。

這隻能使用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: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_xlayout_y價值的任何位置TextView的。但是如何在運行時做到這一點?

我已經試過這樣:

TextView text; 

text=(TextView)findViewById(R.id.percent); 

text.setText("This can be done");會輕易改變在運行時的文本,但我怎麼可以改變從代碼現在的位置?

回答

0

如果通過使用絕對佈局來試試這個,請看下面的代碼。

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

首先,絕對佈局已棄用,請改用相對佈局,Frame-Layout。

然後使用,

LayoutParams params = new LayoutParams(MarginLayoutParams.WRAP_CONTENT, MarginLayoutParams.WRAP_CONTENT); 

如果真的需要進口,採用進口選定的LayoutParams。

在使用

params.leftmargin 

params.rightmargin 

用於改變位置之後。