我在代码视图中添加了两个元素。 imageView和纺车。这两个元素显示但在同一个地方。我希望将ImageView放在屏幕中心的另一个元素之上。
添加视图的代码:
RelativeLayout container= (RelativeLayout)findViewById(R.id.container);
RelativeLayout.LayoutParams position = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
position.addRule(RelativeLayout.CENTER_IN_PARENT);
imgCenter.setLayoutParams(position);
container.addView(imgCenter);
RelativeLayout.LayoutParams position2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
position2.addRule(RelativeLayout.CENTER_IN_PARENT);
position2.addRule(RelativeLayout.BELOW,imgCenter.getId());
spinner.setLayoutParams(position2);
container.addView(spinner);
relativelayout的xml:
<RelativeLayout 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"
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="com.app.exemple.LoadingActivity"
android:id="@+id/container" >
答案 0 :(得分:0)
删除第二个孩子的父母中心
RelativeLayout container= (RelativeLayout)findViewById(R.id.container);
RelativeLayout.LayoutParams position = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
position.addRule(RelativeLayout.CENTER_IN_PARENT);
imgCenter.setLayoutParams(position);
container.addView(imgCenter);
RelativeLayout.LayoutParams position2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
position2.addRule(RelativeLayout.BELOW,imgCenter.getId());
spinner.setLayoutParams(position2);
container.addView(spinner);
答案 1 :(得分:0)
感谢@HareshChhelana,我找到了解决方案。 我按如下方式更改了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"
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="com.app.exemple.LoadingActivity"
android:id="@+id/container"
android:orientation="vertical"
android:gravity="center" >
代码:
LinearLayout container= (LinearLayout)findViewById(R.id.container);
ProgressBar spinner = new ProgressBar(this,null,android.R.attr.progressBarStyleLarge);
container.addView(imgCenter);
container.addView(spinner);