【问题标题】:android XML scale imageview down (without code)android XML缩小imageview(无代码)
【发布时间】:2014-10-02 22:43:29
【问题描述】:

我的 ImageView 有问题。我想在我的 TextView 左侧有一个带有图标的 ImageView,但只有 TextView 的高度。所以它应该按比例缩小,因为图像更大。

这是我的代码:

<RelativeLayout
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal"
   android:padding="5dip" >

   <LinearLayout
       android:id="@+id/icon_container"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignBottom="@+id/name"
       android:layout_alignTop="@+id/name" >

       <ImageView
           android:id="@+id/icon"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:contentDescription="@null"
           android:scaleType="fitStart"
           android:src="@drawable/m1" />
   </LinearLayout>

   <TextView
       android:id="@+id/name"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_toRightOf="@+id/icon_container"
       android:text="Large Text"
       android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

(在没有 LinearLayout 的情况下也可以工作)

但这会在图标和文本之间产生间隙,如下所示:

现在我真的不知道如何解决这个问题。我还尝试了一个带有 Icon 和 TextView 的 LinearLayour,但这也不起作用。

编辑:

我没有找到没有代码的解决方案,但是我调用了代码:

ImageView icon = (ImageView)vi.findViewById(R.id.icon);
TextView name = (TextView)vi.findViewById(R.id.name);

name.measure(MeasureSpec.UNSPECIFIED,MeasureSpec.UNSPECIFIED);
name.setText("Test 1");

int height = name.getMeasuredHeight();
icon.getLayoutParams().height = height;
icon.getLayoutParams().width = height;

【问题讨论】:

    标签: android xml


    【解决方案1】:

    您可以尝试在 TextView 上使用 drawableLeft,这会在 TextView 的左侧放置一个可绘制对象。我认为它可能会随着 TextView 的高度而缩放,如果不是,您应该能够获取 TextView 的高度并自行缩放。

    Programmatically set left drawable in a TextView

    编辑 这里有一些关于获取视图高度的链接

    How to retrieve the dimensions of a view?

    getHeight returns 0 for all Android UI objects

    Android TextView has height and width of 0

    最有用的可能在最后一个问题

    "在 View 被绘制之前求其宽度和高度:

    第一次调用度量”

    view.measure(MeasureSpec.UNSPECIFIED,MeasureSpec.UNSPECIFIED)
    

    "现在您可以使用 getMeasuredWidth 获取宽度,使用 getMeasuredHeight 获取高度"

    int width = view.getMeasuredWidth();
    int height = view.getMeasuredHeight();
    

    【讨论】:

    • 我试过了,但是 textview 会缩放到图像的高度。即使我将宽度设置为例如30dp,图像被剪切,而不是缩放。我也尝试在没有代码的情况下做到这一点,如果可能的话只有 xml。
    • 您可以设置宽度和高度以及比例类型,这适用于很多情况,但不是全部。很多时候,自己缩放图像会更容易,也更有意义。
    • 在某些情况下,这可能是真的,但我将此图像用于不同的屏幕尺寸。当然我可以为 xhdpi、hdpi 制作一个……但在某些屏幕上也会出现问题(当图像的高度大于我的 TextView 的高度时。如果图片更小,它会按比例放大而且看起来不太好。
    • 如果你想在这些情况下进行不同的扩展,你需要使用代码,xml 不会这样做。
    • 我在带有适配器的 ListView 中使用它,我无法在函数 getView() 中获得宽度/高度
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-26
    • 2018-11-26
    • 1970-01-01
    相关资源
    最近更新 更多