几种常用移动控件的方法区分

public class MainActivity extends Activity {
 private Scroller scroller;
 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);父控件没动,控件相对父控件做反向偏移

    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>
   
</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值