繁体   English   中英

在相对布局中按中心线对齐视图

[英]Align views by center line in RelativeLayout

我有根RelativeLayout和子视图。

在一些视图 V1 之后,我想放置 3 个视图: TextView TV1、 TextView TV2 和ProgressBar Progress。 当然,Progress 的高度大于 textviews。 在这 3 个视图之后放置了其他视图。

我如何用“中心”线放置 TV1、TV2、Progress? 不使用任何额外的容器,例如: LinearLayout

在此处输入图片说明

Step-1 创建一个包含这三个视图的LinearLayout并将android:gravity="center"设置为如下所示的 linearlayout

<LinearLayout 
android:id="@+id/action_header_RL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" 
android:gravity="center"
>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="123"/>

 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="456"/>

     <ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
</LinearLayout>

选择一个参考视图(假设在您的情况下为 progressBar)并将其他视图相对于其顶部底部对齐。 这样,它们将相对于其水平轴居中。

android:layout_alignTop ="@+id/reference_progressbar_id"
android:layout_alignBottom ="@id/reference_progressbar_id"

android:centerVertical="true"添加到您想要居中的视图,但这并不能保证它们不会与您添加到布局中的任何其他视图重叠。

保持此属性与 tv1 tv2 和进度

< android:layout_centerVertical="true">

并保持 tv1 android:layout_alignParentLeft="true"

和 tv2 to_Rightof tv1 和进度保持 to_RightOf tv2

    Try This Below Code:

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center" >

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ANDROID"
        android:layout_toLeftOf="@id/TextViewValue" />

    <TextView
        android:id="@+id/TextViewValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="Layout" />

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/TextViewValue" />

</RelativeLayout>

暂无
暂无

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

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