![](/img/trans.png)
[英]Using RelativeLayout to offset center align ProgressBar view?
[英]Cannot center view in RelativeLayout
我正在嘗試在父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);
}
我也嘗試添加layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
在CENTER_IN_PARENT
行之前。 似乎沒有任何作用。 它始終與右下角對齊。
這是我的父視圖:
<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
是因為刪除加載動畫時,我會插入片段並希望它們移到左側。
我已經通過TextViews和ImageViews實現了這一點,方法是將子視圖的右側與父視圖的右側對齊,並將子視圖的左側與父視圖的左側對齊。
<TextView
style="@style/NewTextM2"
android:id="@+id/soft_dialog_info_item_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/<Parent>"
android:layout_alignLeft="@+id/<Parent>"
android:layout_alignRight="@+id/<Parent>"
android:gravity="center"
android:tag="item_text"
android:text="@string/text"
android:maxLines="1" />
這將使視圖與父對象的底部中心對齊。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.