【问题标题】:Limit height of a view to the height of the parent view将视图的高度限制为父视图的高度
【发布时间】:2010-11-10 14:43:30
【问题描述】:

我有一个滚动布局,每行都有一个相对布局。现在有图有文字了,我想把每行的高度限制为文字。

因此,如果我的图像高于文本,则图像将被调整为文本的高度(按比例)。如何在 XML 中做到这一点?

目前线定义看起来像(从mumble android实现复制):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout01"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent">

    <ImageView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/userRowState"
        android:src="@drawable/icon" />
    <TextView
        android:layout_toLeftOf="@+id/userRowStatus"
        android:layout_height="wrap_content"
        android:text="[Name]"
        android:id="@+id/userRowName"
        android:ellipsize="end"
        android:layout_width="wrap_content"
        android:lines="1"
        android:singleLine="true"
        android:textSize="25sp"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/userRowState" />
    <!--
        singleLine is deprecated but it is the only way to get ellipsize work
        it seems. 'inputType text, maxLines 1' didn't work. It's a known issue
        that Google has been unable to reproduce in 1½ years
        http://code.google.com/p/android/issues/detail?id=882 Will see what
        happens first, they remove support for singleLine or they manage to
        reproduce the error. :p
    -->
</RelativeLayout>

真诚的xZise

【问题讨论】:

  • 这是个好问题。顺便说一句,我想不出一种方法可以在 XML 布局中执行此操作,尽管以编程方式执行此操作似乎相当容易。我想知道您是否可以只使用 ViewStub 而不是 ImageView,然后在您的 onCreate 方法中,根据需要将它们膨胀,将 TextView 的高度作为 ImageView 的 LayoutParams 传递?从那里,您可以根据需要调整 ImageView 的 scaleType。
  • 我不认为你可以这样做。为什么不在您的案例中设置父项目的 layout_height 相对布局并在您的子项目中使用 fill_parent。

标签: android android-layout


【解决方案1】:

您可以尝试将图标的顶部和底部与 userRowName 的顶部和底部对齐:

<ImageView
    android:id="@+id/userRowState"
    android:src="@drawable/icon"
    android:layout_width="wrap_content"
    android:layout_alignTop="@+id/userRowName"
    android:layout_alignBottom="@+id/userRowName"/>

这应该使它在文本减小时减小。不过,它将保持在其旧位置的水平居中,并且由于水平方向受到限制,因此不会增长。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-11
    • 2020-04-14
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多