如何在android中以编程方式更改relativelayout中的x视图
我在RelativeLayout中有一个视图,我想将视图的位置更改为RelativeLayout的中间位置: 我做什么 xml:如何在android中以编程方式更改relativelayout中的x视图,android,android-layout,android-relativelayout,Android,Android Layout,Android Relativelayout,我在RelativeLayout中有一个视图,我想将视图的位置更改为RelativeLayout的中间位置: 我做什么 xml: <RelativeLayout android:id="@+id/partieoption" android:layout_width="fill_parent" android:layout_height="fill_parent" android:weightSum="6" android:orientation="h
<RelativeLayout
android:id="@+id/partieoption"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="6"
android:orientation="horizontal" >
<ImageView
android:id="@+id/photo2"
android:src="@drawable/loglog"
android:layout_width="50dp"
android:layout_height="50dp"
/>
</RelativeLayout>
要移动视图,请添加此代码
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
RelativeLayout partieoption = (RelativeLayout) findViewById(R.id.partieoption);
ImageView iv = (ImageView) findViewById(R.id.photo2);
ObjectAnimator transAnimation= ObjectAnimator.ofFloat(iv, x, iv.getX(), width/2);
transAnimation.setDuration(3000);
transAnimation.start();
您需要为您的ImageView
创建一个带有中心的父规则,然后使用setLayoutParams(ViewGroup.LayoutParams params)
应用它
下面是一个例子:
ImageView imageView = (ImageView)findViewById(R.id.iv_image);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)imageView.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
imageView.setLayoutParams(layoutParams);
在xml中使用重力属性,如下所示:
android:layout_gravity="center"
<RelativeLayout
android:id="@+id/partieoption"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="6"
android:orientation="horizontal" >
<ImageView
android:id="@+id/photo2"
android:src="@drawable/loglog"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
/>
</RelativeLayout>
在你的情况下,我认为会是这样的:
android:layout_gravity="center"
<RelativeLayout
android:id="@+id/partieoption"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="6"
android:orientation="horizontal" >
<ImageView
android:id="@+id/photo2"
android:src="@drawable/loglog"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
/>
</RelativeLayout>
该视图在哪里?你是在谈论你的图像视图吗?请将您的完整XmlNoooo发布在relativelayout的中间,而不是scran的中间!!因此,assign width=partieoption.getWidth()和rest保持不变。