2010-11-10 158 views
1

我在每一行中都有一个相对布局的滚动布局。现在有一个图像和一个文本,我想限制每行的高度到文本。将视图的高度限制为父视图的高度

所以如果我的图像高于文本,图像将被调整到文本的高度(比例)。我怎样才能在XML中做到这一点?

目前该行定义的样子(从咕哝机器人执行复制):

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

+0

这是一个很好的问题。 Offhand,我想不出一种可以在XML布局中做到这一点的方式,尽管看起来编程方式很容易。我想知道你是否可以使用ViewStub代替ImageView,然后在你的onCreate方法中,根据需要膨胀它们,传递TextView的高度作为ImageView的LayoutParams?从那里,你可以根据需要调整你的ImageView的scaleType。 – kcoppock 2010-11-10 15:50:38

+0

我不认为你可以这样做。为什么不在你的案例中设置父项的layout_height相对布局,并在你的子项中使用fill_parent。 – 2010-11-10 15:58:05

回答

0

你可以尝试排列图标的顶部和底部到顶部和底部用户地址:

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

当文本减少时,这应该会减小大小。它将保持水平位置在原来的位置,但不会随着水平方向的限制而增长。