需注意 TextView 需有下面兩個屬性才能移動
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:text="@string/hello_world" />
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="112dp"
android:text="Left" />
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignParentRight="true"
android:layout_marginRight="63dp"
android:text="Right" />
2.撰寫以下程式碼
package com.example.sampleuicontrol;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView txtView;
private Button btnLeft;
private Button btnRight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtView = (TextView)findViewById(R.id.textView1);
btnLeft = (Button)findViewById(R.id.button1);
btnRight = (Button)findViewById(R.id.button2);
btnLeft.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//取得 TextView 的Layout,必須注意每種Layout的屬性都不一樣
RelativeLayout.LayoutParams layoutParams = (LayoutParams) txtView.getLayoutParams();
//每次往左移開10dp
layoutParams.leftMargin -=10;
//再把Layout的參數放回 TextView
txtView.setLayoutParams(layoutParams);
}
});
btnRight.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//取得 TextView 的Layout,必須注意每種Layout的屬性都不一樣
RelativeLayout.LayoutParams layoutParams = (LayoutParams) txtView.getLayoutParams();
//每次往左移進10dp
layoutParams.leftMargin +=10;
//再把Layout的參數放回 TextView
txtView.setLayoutParams(layoutParams);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
3.往左移動沒辦法貼齊 Device 邊界的關係,是因為 xml 有下列參數
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
4. 須注意每種Layout的屬性都不一樣
RelativeLayer
LinearLayout
沒有留言:
張貼留言