将 Android 相对布局项目与不同线性布局中的动态大小项目对齐

Align Android Relative Layout Items w/ Dynamic Size Item in Different Linear Layout

我还没有找到任何有关将小部件从 RelativeLayout 对齐到水平 LinearLayout 中的小部件的信息。从一种布局对齐到另一种布局几乎没有在 Internet 上提及。我认为我的问题可能最容易用图片来解释。画的粗糙请见谅:

这真的总结了我想要做的事情。绿色是相对布局,蓝色是水平线性布局,每个项目的宽度为 'match_parent',权重为“1”,以便在线性布局中均匀分布 - 这样做的目的是利用通过扩展项目 B-E 来获得更宽的屏幕和横向模式,这是我的应用程序中最重要的信息。

我在线性布局的左侧有一个静态大小的图像 (A)。线性布局相对于 A 的右侧放置 5dp。项目 B-D 的 rightMargin 为 8dp 用于间距。 F 是一对与 A 开头对齐的 TextView。G 是一对与屏幕右侧对齐的 TextView。

我想做的是:

我已尝试将相对布局小部件的 alignEnd、alignToEnd 和 alignRight 与线性布局中的小部件对齐,但 Android Studio 拒绝这样做,因为它们不是 RelativeLayout 中的兄弟。我可以通过编程方式执行此操作并在 PreDrawListener 中计算它(添加 A 的宽度 + 5dp + B 的宽度),但如果可能的话,我宁愿在布局中执行此操作。

这是我的布局 xml:

<?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="210dp"
    android:background="#FFFFFF">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="1dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="9dp"
        android:cropToPadding="false"
        android:src="@drawable/avatar"/>

    <TextView
        android:id="@+id/Name"
        android:layout_width="wrap_content"
        android:layout_height="31dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="155dp"
        android:ellipsize="end"
        android:gravity="center_vertical|left"
        android:paddingTop="3dp"
        android:text="Name"
        android:textColor="#000000"
        android:textSize="22sp"/>

    <TextView
        android:id="@+id/id"
        android:layout_width="150dp"
        android:layout_height="24dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="185dp"
        android:ellipsize="end"
        android:gravity="top|left"
        android:paddingTop="-5dp"
        android:text="ID"
        android:textColor="#000000"
        android:textSize="18sp"/>

    <TextView
        android:id="@+id/age"
        android:layout_width="150dp"
        android:layout_height="24dp"
        android:layout_marginLeft="155dp"
        android:layout_marginTop="185dp"
        android:ellipsize="end"
        android:gravity="top|left"
        android:paddingTop="-5dp"
        android:text="Age"
        android:textColor="#000000"
        android:textSize="18sp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="159dp"
        android:layout_marginLeft="160dp"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginRight="8dp"
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                android:id="@+id/pTitle"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:gravity="center"
                android:text="Title"
                android:textColor="#ffffffff"
                android:textSize="14sp"
                android:textStyle="bold"/>

            <com.github.mikephil.charting.charts.LineChart
                android:id="@+id/chart1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginRight="8dp"
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                android:id="@+id/rTitle"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:gravity="center"
                android:text="Title"
                android:textColor="#ffffffff"
                android:textSize="14sp"
                android:textStyle="bold"/>

            <com.github.mikephil.charting.charts.LineChart
                android:id="@+id/chart2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginRight="8dp"
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                android:id="@+id/hTitle"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:gravity="center"
                android:text="Title"
                android:textColor="#ffffffff"
                android:textSize="14sp"
                android:textStyle="bold"/>

            <com.github.mikephil.charting.charts.LineChart
                android:id="@+id/chart3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                android:id="@+id/sTitle"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:gravity="center"
                android:text="Title"
                android:textColor="#ffffffff"
                android:textSize="14sp"
                android:textStyle="bold"/>

            <com.github.mikephil.charting.charts.LineChart
                android:id="@+id/chart4"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

这可能吗?谢谢

好的,我是这样解决的:

// Pre-Draw Task
ViewTreeObserver userImageVTO = userImage.getViewTreeObserver();
userImageVTO.addOnPreDrawListener( new ViewTreeObserver.OnPreDrawListener() {
    public boolean onPreDraw() {

        // Set Alignment Widths
        name.setWidth( imageView.getWidth() + ( ( imageView.getWidth() / 150 ) * 5 ) + chart1.getWidth() );  // Get the Pixel Density (Since Image is 150dp) for 5dp of Padding
        age.setWidth( chart1.getWidth() );

        return true;
    }
});

这是在我的自定义 ListAdapter 的 getView() 中。我用已知大小的 imageView(150dp 正方形)和填充 (5dp) 加上检索到的 chart1 的宽度计算了 F 的宽度。然后,在 XML 中,我将其添加到 G:

android:layout_toRightOf="@id/age"
android:layout_marginLeft="8dp"

这允许 G 在旋转时 expand/contract,因为它设置为始终在 F 右侧 8dp。