Vertically centering two TextViews in a RelativeLayout

回眸只為那壹抹淺笑 提交于 2019-12-24 17:24:27

问题


What's Happening

I am writing a PopupWindow containing two TextViews, where the second TextView should be centered vertically in the popup, and the first TextView should be directly above it.

The problem is that the RelativeLayout seems to be treating the two TextViews as a single element, and vertically centering the middle of them. I want the lower TextView to be the one centered, though, and the upper one to be resting just above it (hence the android:layout_above="@id/first_name").

XML Layout

Note the apparently unnecessary LinearLayout there because the RelativeLayout refused to completely vertically fill the screen (the PopupWindow is using ViewGroup.LayoutParams.MATCH_PARENT).

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" >

        <TextView
            android:id="@+id/first_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:singleLine="true"
            android:text="Christopher" />

        <TextView
            android:id="@+id/lbl_hello"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@id/first_name"
            android:gravity="center"
            android:singleLine="true"
            android:text="@string/hello" />
    </RelativeLayout>

</LinearLayout>

Java Activity

LayoutInflater inflater = LayoutInflater.from(this);
final View popupView = inflater.inflate(R.layout.<fragment name>,
                                        <parent object>,
                                        false);
final PopupWindow pwindow = new PopupWindow(popupView,
                                            ViewGroup.LayoutParams.MATCH_PARENT,
                                            ViewGroup.LayoutParams.MATCH_PARENT,
                                            true);
pwindow.setTouchable(true);
new Handler().postDelayed(new Runnable() {

    @Override
    public void run() {
        pwindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
    }

}, 100L);

回答1:


As Sushil said, you can probably remove your LinearLayout completely. The following fix should work; if it does, try removing the linear layout as well.

The issue here is the android:gravity="center" on the RelativeLayout. If you remove that, you can get the placement you desire:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >



回答2:


You can remove your Linearlayout simply. Right now your textview are child views of Relativelayout which is a child view to LinearLayout.

        <?xml version="1.0" encoding="utf-8"?>
       <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" >

        <TextView
            android:id="@+id/first_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:singleLine="true"
            android:text="Christopher" />

        <TextView
            android:id="@+id/lbl_hello"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@id/first_name"
            android:gravity="center"
            android:singleLine="true"
            android:text="@string/hello" />
    </RelativeLayout>



回答3:


Use this xml:

   <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="40dp"
                android:layout_marginRight="40dp"
                android:layout_marginTop="90dp">

                <TextView
                    android:id="@+id/inv"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:text="@string/invfam"
                    android:textColor="#f5f5f5"
                    android:textSize="20dp" />

                <TextView
                    android:id="@+id/promo"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/inv"
                    android:gravity="center"
                    android:layout_marginTop="10dp"
                    android:text="Enjoy an additional 15 AED off your first video consultation. Sign up for a Health at Hand account and you will be credited 30 AED as a new user plus an extra 15 AED! #feel better today"
                    android:textColor="#f5f5f5"
                    android:textSize="14dp" />

            </RelativeLayout>


来源:https://stackoverflow.com/questions/22161456/vertically-centering-two-textviews-in-a-relativelayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!