简体   繁体   English

如何将两个文本视图居中为相对布局中的中心

[英]How to center two text views as center aligned in relative layout

How to place two text views Marked within red rectangle in relative layouts with center aligned. 如何放置两个文本视图Marked within red rectangle内的相对布局中,中心对齐。 How can I design as shown in the picture? 我如何设计如图所示? Here text and number both are textviews. 这里的文字和数字都是textviews。

在此处输入图片说明

I have tried the following but I could not get center aligned view to relative layout. 我尝试了以下操作,但无法将中心对齐的视图设置为相对布局。

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="50dp">

            <LinearLayout
                android:id="@+id/location_party_size_lbl_layer"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_centerInParent="true"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/location_party_size_lbl"
                    android:layout_width="170dp"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:gravity="center_horizontal"
                    android:maxLines="2"
                    android:text="@string/loc_estimate"
                    android:textColor="@android:color/white"
                    android:textSize="15sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:layout_centerVertical="true"
                android:layout_toRightOf="@+id/location_party_size_lbl_layer"
                android:orientation="horizontal"
                android:paddingBottom="10dp"
                android:paddingTop="10dp"
                android:weightSum="1.0">

                <TextView
                    android:id="@+id/location_party_size"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.48"
                    android:background="@drawable/rounded_bg"
                    android:gravity="center"
                    android:textColor="@android:color/white" />

                <TextView
                    android:id="@+id/qnowscreen_dummy_tv"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.52"
                    android:visibility="invisible" />
            </LinearLayout>
        </RelativeLayout>

You can do that by creating an anchor view and aligning TextView s relatively to the anchor. 您可以通过创建锚视图并将TextView与锚相对对齐来实现。

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

    <View
      android:id="@+id/anchor"
      android:layout_width="0dp"
      android:layout_height="0dp"
      android:layout_centerInParent="true" />

    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_above="@id/anchor"
      android:layout_centerHorizontal="true" />

    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@id/anchor"
      android:layout_centerHorizontal="true" />

  </RelativeLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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