android控件的移动
⑴ 在android中如何改变控件的坐标(例如一个button,我想让它向右移动20dp)
RelativeLayout.LayoutParams ballLp = (android.widget.RelativeLayout.LayoutParams)b.getLayoutParams();
ballLp.leftMargin = 从你控件的基础上加20像素;
ballLp.topMargin = 不变:
b.setLayoutParams(ballLp);
这没有移动的动画,只是直接跳到次位置上
要有移动的话可以加动画,或者让这段代码执行20次,每次leftMargin增加1就可以了。
⑵ android开发如何实现一个控件移动,其他控件
可以看看android studio 2.2里面新出的ConstraintLayout布局,可以给各个控件添加相互之间的约束,以达到移动一个控件,另一个控件跟着移动的效果。
另外,这个约束不仅是可以在布局的时候直接通过xml添加,也可以自定义约束变化,以达到更炫的效果。
⑶ 在android页面上为什么添加的控件都不能移动
把最外层的容器改为Relativelayout,就可以拖动了。
只是在android上基本不用拖动的方式来布局的,这对你以后没什么好处。还是老老实实学习xml布局吧,以后你就知道为什么拖不可取了。
⑷ android怎么移动按钮
刚才手机在画面的显示里面,点击移动按钮就可以移动按钮。
⑸ android 控件移动方法
Android控件位置都是自己设置的。
如果要控件放到任意位置,建议用RelativeLayout(相对布局)
然后给控件添加属性,把控件放到想要的位置
下面是相对布局中 控件能用到的属性。
属性值为true可false
android:layout_centerHrizontal 水平居中
android:layout_centerVertical 垂直居中
android:layout_centerInparent 相对于父元素完全居中
android:layout_alignParentBottom 贴紧父元素的下边缘
android:layout_alignParentLeft 贴紧父元素的左边缘
android:layout_alignParentRight 贴紧父元素的右边缘
android:layout_alignParentTop 贴紧父元素的上边缘
属性值必须为id的引用名
android:layout_below 在某元素的下方
android:layout_above 在某元素的上方
android:layout_toLeftOf 在某元素的左边
android:layout_toRightOf 在某元素的右边
android:layout_alignTop 本元素的上边缘和某元素的上边缘对齐
android:layout_alignLeft 本元素的左边缘和某元素的左边缘对齐
android:layout_alignBottom 本元素的下边缘和某元素下边缘对齐
android:layout_alignRight 本元素的右边缘和某元素右边缘对齐
属性值为具体的像素值班,如30dp
android:layout_marginBottom 离某元素底边缘的距离
android:layout_marginLeft 离某元素左边缘的的距离
android:layout_marginRight 离某元素的右边缘的距离
android:layout_marginTop 离某元素上边缘的距离
⑹ android手势如何平移控件
解决方案1:
重写该控件的onTouch方法,move状态设置该view的margin或者在放手状态up中设置不需要手势监听吧,在该方法中判断,是down状态记录按下的位置,控件移动一般都是相对布局
解决方案2:
ontounchListener监听事件
解决方案3:
scroller调用scrollTo
知识点延伸阅读:
控件平移划过屏幕(Scroller简单使用)
MainActivity如下:
package cc.cn;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
/
Demo描述:
Scroller使用示例——让控件平移划过屏幕
注意事项:
1 在布局中将cc.cn.LinearLayoutSubClass的控件的宽度设置为"fill_parent"
便于观察滑动的效果
/
public class MainActivity extends Activity {
private Button mButton;
private LinearLayoutSubClass mLinearLayoutSubClass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init(){
mLinearLayoutSubClass=(LinearLayoutSubClass) findViewById(R.id.linearLayoutSubClass);
mButton=(Button) findViewById(R.id.button);
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
mLinearLayoutSubClass.beginScroll();
}
});
}
}
LinearLayoutSubClass如下:
package cc.cn;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.Scroller;
/**
* API注释:
*
* 1 //第一,二个参数起始位置;第三,四个滚动的偏移量;第五个参数持续时间
* startScroll(int startX, int startY, int dx, int dy, int ration)
*
* 2 //在startScroll()方法执行过程中即在ration时间内computeScrollOffset()
* 方法会一直返回true,但当动画执行完成后会返回返加false.
* computeScrollOffset()
*
* 3 当执行ontouch()或invalidate()或postInvalidate()均会调用该方法
* computeScroll()
*
*/
public class LinearLayoutSubClass extends LinearLayout {
private Scroller mScroller;
private boolean flag=true;
public LinearLayoutSubClass(Context context) {
super(context);
}
public LinearLayoutSubClass(Context context, AttributeSet attrs) {
super(context, attrs);
//也可采用该构造方法传入一个interpolator
//mScroller=new Scroller(context, interpolator);
mScroller=new Scroller(context);
}
@Override
public void computeScroll() {
super.computeScroll();
if(mScroller.computeScrollOffset()){
scrollTo(mScroller.getCurrX(), 0);
//使其再次调用computeScroll()直至滑动结束,即不满足if条件
postInvalidate();
}
}
public void beginScroll(){
if (flag) {
mScroller.startScroll(0, 0, -2500, 0, 2500);
flag = false;
} else {
mScroller.startScroll(0, 0, 0, 0, 1500);
flag = true;
}
//调用invalidate();使其调用computeScroll()
invalidate();
}
}