【发布时间】:2014-07-17 10:51:32
【问题描述】:
我试图在父 RelativeLayout 中居中视图:
private void addLoadingAnimation() {
RelativeLayout mainView = (RelativeLayout) findViewById(R.id.medical_supply_tile_activity);
mainView.removeAllViews();
GifView gifView = new GifView(this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
gifView.setId(R.id.loading_gif);
mainView.setBackgroundColor(getResources().getColor(android.R.color.background_light));
mainView.addView(gifView,layoutParams);
}
我也尝试在CENTER_IN_PARENT 行之前添加layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);。似乎没有任何工作。它始终与右下角对齐。
这是我的父视图:
<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:background="@android:color/background_light"
tools:context=".MedecineTileActivity"
android:id="@id/medical_supply_tile_activity"
android:layout_gravity="left">
</RelativeLayout>
gravity=left是因为去掉加载动画的时候,我插入了片段,想让它们往左边走。
【问题讨论】:
-
在你里面添加
android:gravity="center"RelativeLayout
标签: java android android-layout android-relativelayout