灵活的水平布局,带有`layout_weight`和`maxWidth`

jer*_*rry 6 xml android android-layout android-layout-weight

我有一个TextView和一个正方形ImageView,我想以水平线性布局显示.每个应该占父视图宽度的一半,高度应该适合内容(即图像).文本应垂直居中.

附加约束是图像不应超过给定maxWidth(= maxHeight),并且应该使超出的宽度可用TextView.显然,这与上面的50/50规则相冲突.有没有办法优先考虑约束,即允许图像占用一半的空间,除非它超过给定的大小?

期望的结果

这些是我尝试过的布局:

第一个很好地将左侧拉伸到可用空间.但是图像占用宽度的一半以上,因为layout_weight未指定(如下图所示).

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center_vertical">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Some text."/>
    </LinearLayout>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxWidth="200dp"
        android:adjustViewBounds="true"
        android:src="@drawable/image" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

没有<code>layout_weight</code>到<code>ImageView</code>它始终把宽度的一半,而忽略了<code>maxWidth</code>(见下面的图片).</p>

<pre><code>    <ImageView
        android:layout_width=Run Code Online (Sandbox Code Playgroud)

使用<code>ImageView</code>在另一个<code>LinearLayout</code>中<code>maxWidth</code>再次启用,但封闭<code>LinearLayout</code>仍然占用一半的可用空间(见下图).</p>

<pre><code>    <LinearLayout
        android:layout_width=Run Code Online (Sandbox Code Playgroud)

使用封闭的<code>RelativeLayout</code>带有不可见视图的a作为中心分隔符,但是将分区锁定为50/50(或放置分隔符的任何位置).</p></p>
    </div>
  </div>

<div class=

AL.*_*AL. 2

好的,继续前进并发布我创建的 xml,它非常简单,它可以满足您的大部分条件。我遇到麻烦的一件事是每个视图占用父视图宽度一半的部分。希望这仍然可以在某种程度上帮助您。

样本布局.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="wrap_content"
    android:layout_margin="2dp"
    android:background="@android:color/darker_gray">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/img_sample"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="2dp"
        android:layout_toLeftOf="@id/img_sample"
        android:background="@color/colorPrimary"
        android:gravity="center_vertical"
        android:text="Some text." />

    <ImageView
        android:id="@id/img_sample"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="2dp"
        android:adjustViewBounds="true"
        android:maxWidth="200dp"
        android:src="@drawable/sample_img" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

这是一个示例屏幕截图: 在此输入图像描述

如果您知道如何将每件事分成一半,请在这里发表评论,很高兴了解它以供将来使用。:)

PS:你有没有考虑过以编程方式完成每件事的一半?就像检查LayoutParamsthen 动态设置weightSumlayout_weights 一样。

从此链接检索到的示例图像