【问题标题】:How to let an image define the layout's with in Android如何让图像在 Android 中定义布局
【发布时间】:2014-09-18 23:41:15
【问题描述】:

我想要一个图像来定义布局的边界,并且需要所有其他元素来定位它们自己。

我的问题是我希望文本在到达图像末尾时换行。知道如何通过布局 xml 解决这个问题,还是我必须通过代码来实现?

我当前的布局是这样的:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/KHFGreen"
        android:padding="3dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:layout_gravity="left|center_vertical">

        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/five"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true" />

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:text="Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore. "
            android:layout_gravity="left|center_vertical"
            android:nestedScrollingEnabled="false" />

    </FrameLayout>
</FrameLayout>

非常感谢!

【问题讨论】:

  • 对图像视图使用 matchparent,对文本使用 android:ellipsize="end"
  • 感谢您的回答,但不幸的是,这并没有改变任何事情:/
  • 我可以看到你只想要你的布局的边框然后你使用图像来做同样的事情?
  • 是的,这个布局将在 ListViewAdapter 中膨胀。我想要一个带有绿色边框的图像,并且该图像顶部有一个文本。但是文本到达图像末尾时应该换行......
  • 我不确定仅使用 xml 布局是否可行。我认为您需要以编程方式解决此问题。

标签: android xml android-layout android-inflate android-adapterview


【解决方案1】:

在 oncreate 方法中添加以下逻辑。

imageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

    @Override
    public void onGlobalLayout() {

        textView.getLayoutParams().width = imageView.getMeasuredWidth();

        if (Build.VERSION.SDK_INT >= 16) {
            imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        } else {
            imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        }
    }
});

当我删除 android:nestedScrollingEnabled="false" 并尝试时,我得到了你的要求 而且我也无法添加 android:nestedScrollingEnabled="false",“错误:在包 'android' 中找不到属性 'nestedScrollingEnabled' 的资源标识符”

【讨论】:

  • 你好。感谢您的努力,但不幸的是,这也不是我所需要的。由于我的屏幕截图需要一个不比图像宽的 TextView 并在图像结束的地方换行。有什么想法吗?
  • 嗨 einRobby,它可以通过编程方式完成,而不是在 xml 中完成。
  • 是的,当以编程方式进行时,它可以工作。谢谢你们的帮助。到目前为止,我一直在为 Windows (Phone) 8 开发,并且被那里的设计工具宠坏了:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多