【问题标题】:Align Android Relative Layout Items w/ Dynamic Size Item in Different Linear Layout在不同的线性布局中将 Android 相对布局项目与动态大小项目对齐
【发布时间】:2015-05-07 06:08:08
【问题描述】:

在将小部件从 RelativeLayout 对齐到水平 LinearLayout 中的小部件时,我还没有找到任何东西。互联网上几乎没有提到从一种布局对齐到另一种布局。我认为我的问题可能最容易用图片来解释。粗略的图请见谅:

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

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

我想做的是:

  • 将 F 的右(端)与 B 的右(端)对齐。
  • 将 G 的左侧(开始)与 C 的左侧(开始)对齐。
  • 在设备旋转到横向模式时保持这种关系。

我已尝试将相对布局小部件的 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>

这可能吗?谢谢。

【问题讨论】:

  • 发布你目前尝试过的xml布局
  • 无法将ViewsViewGroups 对齐。我的建议是您尝试从等式中删除LinearLayout。将所有Views 放在RelativeLayout 内,然后您可以正确对齐它们。如果这不是一个选项,您可以随时设置要以编程方式对齐的Views 的位置和大小。但是找到一个只依赖 xml 的解决方案显然更可取。
  • 我可能会使用自定义 ViewGroup 类和自己的 onLayout 方法实现
  • @Xaver,如果 B-E 不在水平 LinearLayout 中,如何允许它们展开?如果我只是在它们周围设置边距,它们会扩大吗?不幸的是,这个网站似乎不太擅长发布 XML...
  • 您使用的是什么 IDE?该xml严重混乱。里面有很多非法字符,比如,而不是"。 Android Studio 或 IntellJ 甚至不将其识别为 xml,更不用说布局 xml...

标签: android android-layout android-linearlayout android-relativelayout


【解决方案1】:

好的,我是这样解决的:

// 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 旋转扩展/收缩,因为它被设置为始终位于 F 右侧 8dp。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-13
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 2018-03-27
    相关资源
    最近更新 更多