【问题标题】:Vertically centering two TextViews in a RelativeLayout在 RelativeLayout 中垂直居中两个 TextView
【发布时间】:2018-02-20 15:52:15
【问题描述】:

发生了什么

我正在编写一个包含两个 TextViews 的 PopupWindow,其中第二个 TextView 应该在弹出窗口中垂直居中,第一个 TextView 应该在它的正上方。

问题在于,RelativeLayout 似乎将两个 TextView 视为一个元素,并将它们的 中间 垂直居中。不过,我希望下方的 TextView 居中,而上方的 TextView 位于其上方(因此是android:layout_above="@id/first_name")。

XML 布局

请注意那里明显不必要的 LinearLayout,因为 RelativeLayout 拒绝完全垂直填充屏幕(PopupWindow 正在使用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 活动

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);

【问题讨论】:

    标签: android android-layout android-relativelayout


    【解决方案1】:

    正如 Sushil 所说,您可能可以完全删除您的 LinearLayout。以下修复应该有效;如果是这样,请尝试删除线性布局。

    这里的问题是RelativeLayout 上的android:gravity="center"。如果你删除它,你可以获得你想要的展示位置:

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

    【讨论】:

    • 这是重力,你是对的!我用子元素上的android:layout_centerHorizontal="true" 替换了RelativeLayout 上的android:gravity="center",一切都排好了。谢谢!
    【解决方案2】:

    您可以简单地删除您的线性布局。现在你的 textview 是 Relativelayout 的子视图,它是 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】:

      使用这个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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-19
        • 1970-01-01
        • 2013-10-25
        • 1970-01-01
        • 2023-03-27
        • 2019-12-30
        • 2012-11-24
        • 1970-01-01
        相关资源
        最近更新 更多