繁体   English   中英

无法在RelativeLayout中居中显示

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM