Android绑定(bind)xml中两个元素的宽度和/或高度

标签 android

假设有这样一个 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" />

最终结果是这样的: enter image description here

所以问题是:有没有办法在 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 可以在线性布局之外

it can be even this way

提前致谢

最佳答案

你可以这样做-

 <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/

相关文章:

Android:替换 WebView 中的 HTML 标签

android - 在 Android 按钮中垂直居中超大文本

android - NumberPicker Android 6.0 如何增加字体大小?

Android - 我们应该将额外的业务逻辑放在架构中的什么位置?

java - 膨胀类 NavigationView 时出错

android - 在项目中使用 ActionBarSherlock 的问题

java - 我不明白为什么我的可扩展 ListView 的 onChildClick 方法出现致命异常

java - 更改事件监听器中的变量?

android - 如何在 android 中以编程方式(通过代码,而不是 xml)使文本发光?

android - 使用 Volley 获取 JSONArray