在RelativeLayout中将两个TextView垂直居中

在RelativeLayout中将两个TextView垂直居中

本文介绍了在RelativeLayout中将两个TextView垂直居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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").

请注意那里显然不必要的LinearLayout,因为RelativeLayout拒绝完全垂直填充屏幕(PopupWindow使用ViewGroup.LayoutParams.MATCH_PARENT).

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活动

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

推荐答案

正如Sushil所说,您可能可以完全删除LinearLayout.以下修复程序应该起作用;如果可以,请尝试删除线性布局.

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.

这里的问题是RelativeLayout上的android:gravity="center".如果将其删除,则可以获取所需的展示位置:

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" >

这篇关于在RelativeLayout中将两个TextView垂直居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 09:43