public class MainActivity extends Activity {
private Scroller scroller;
private Button bt;
private RelativeLayout rl;
private Button bt;
private RelativeLayout rl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scroller = new Scroller(this);
bt = (Button)findViewById(R.id.bt);
rl = (RelativeLayout)findViewById(R.id.rl);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/*((View) v.getParent()).scrollTo(200,0);父控件没动,控件相对父控件做反向偏移
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scroller = new Scroller(this);
bt = (Button)findViewById(R.id.bt);
rl = (RelativeLayout)findViewById(R.id.rl);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/*((View) v.getParent()).scrollTo(200,0);父控件没动,控件相对父控件做反向偏移
v.scrollTo(200,0); 控件没动,控件内容相对控件做反向偏移
((View) v.getParent()).layout(100, 0,200, 600);父控件相对再上级父控件做正向偏移
v.layout(0, 0,200, 600);子控件相对父控件做正向偏移
((View) v.getParent()).offsetLeftAndRight(300);父控件相对再上级父控件做正向偏移
v.offsetLeftAndRight(30);子控件相对父控件做正向偏移*/
/* LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) rl.getLayoutParams();
params.leftMargin = rl.getLeft()+200;
params.topMargin = rl.getTop() + 50;
rl.setLayoutParams(params);先拿到本身在父控件的Params,通过修改params参数改变自身相对父控件的位置 */
/*RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) v.getLayoutParams();
params.leftMargin = v.getLeft()+200;
params.topMargin = v.getTop() + 200;
v.setLayoutParams(params);同上*/
}
});
}
}
xml如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.provider.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="哈哈哈"/>
<RelativeLayout
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00ffff">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清华"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="18sp"
android:background="#ff0000" />
<Button
android:id="@+id/bt"
android:layout_below="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffff00"
android:text="滑动吧"
/>
</RelativeLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.provider.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="哈哈哈"/>
<RelativeLayout
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00ffff">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清华"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="18sp"
android:background="#ff0000" />
<Button
android:id="@+id/bt"
android:layout_below="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffff00"
android:text="滑动吧"
/>
</RelativeLayout>
</LinearLayout>