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

时间:2014-06-16 12:09:31

标签: 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

所以问题是:有没有办法将h2绑定到xml中的h1(NOT PROGRAMMATICALLY),所以每当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

提前致谢

4 个答案:

答案 0 :(得分:1)

你可以这样做 -

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

因此解决方案是在容器视图对应用程序无用时禁用基线对齐。然后将每个视图的布局权重设置为相等。

另外,我建议您查看这篇文章 - shifty-baseline-alignment

答案 1 :(得分:0)

尝试设置此值:

<TextView
    android:id="@+id/title"
    style="@style/label_text_dark"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="8"
    android:gravity="center_vertical" />

<View
    android:id="@+id/v_line"
    android:layout_width="@dimen/margin_extra_small"
    android:layout_height="match_parent"
    android:background="@color/light_gray" />

LinearLayout的高度是正确的。

答案 2 :(得分:0)

您可以尝试这样:

<TextView
    android:id="@+id/title"
    style="@style/label_text_dark"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="8"
    android:gravity="center_vertical" />

<View
    android:id="@+id/v_line"
    android:layout_width="@dimen/margin_extra_small"
    android:layout_height="wrap_content"
    android:layout_alignTop="@id/title"
    android:background="@color/light_gray" />

alignTop会在TextView高度变化时调整视图大小。祝你好运

答案 3 :(得分:0)

您可以使用RelativweLayout ..根据需要修复视图宽度

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

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"

    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="8"
    android:background="#119283"
    android:gravity="center_vertical" />

<View
    android:id="@+id/v_line"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/title"
     android:layout_weight="3"
    android:layout_alignBottom="@+id/title"
    android:layout_toRightOf="@+id/title"
    android:background="#342748" />