假设有这样一个 XML 布局:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="horizontal"
android:paddingLeft="@dimen/margin_medium" >
<TextView
android:id="@+id/title"
style="@style/label_text_dark"
android:layout_width="0dp"
android:layout_height="______h1_____"
android:layout_weight="8"
android:gravity="center_vertical" />
<View
android:id="@+id/v_line"
android:layout_width="@dimen/margin_extra_small"
android:layout_height="______h2_____"
android:background="@color/light_gray" />
最终结果是这样的:
所以问题是:有没有办法在 xml 中将 h2 绑定(bind)到 h1(非编程方式),因此每当 h1 更改时,h2 会自动更改。
我要查找的内容应该与此类似:
<View
android:id="@+id/v_line"
android:layout_width="@dimen/margin_extra_small"
android:layout_height="@+id/title.height"
android:background="@color/light_gray" />
android:layout_height="@+id/title.height"(这当然不行)
注意:其中一个元素也可以在父容器之外,例如v_line 可以在线性布局之外
提前致谢
最佳答案
你可以这样做-
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_baselineAligned="false"
android:orientation="horizontal"
android:background="@color/white">
<TextView
android:id="@+id/title"
style="@style/label_text_dark"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Last" />
<View
android:id="@+id/v_line"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/light_gray" />
</LinearLayout>
因此解决方案是在容器 View 对您的应用程序无用时禁用它的基线对齐。然后设置每个 View 的布局权重相等。
另外,我鼓励您查看这篇文章 - shifty-baseline-alignment .
关于Android绑定(bind)xml中两个元素的宽度和/或高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24243402/