我在视图中为代码添加了两个元素。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" >
感谢@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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句