【问题标题】:How to guarantee space for an ImageView, keep it from shrinking?如何保证 ImageView 的空间,使其不缩小?
【发布时间】:2023-04-08 11:52:01
【问题描述】:

我需要创建一个包含以下内容的 XML:
* 两个具有不同文本(1-3 行)的 TextView,一个 TextView 在另一个下方。
* 2 个 TextView 右侧的 1 个 ImageView,垂直居中,30x30 像素。

一个主要的限制是它在充气到PopupWindow时不能占据整个屏幕,这意味着我不能在很多地方使用属性fill_parent。

我尝试了很多不同的布局,但是当文本很长时,ImageView 总是被 TextViews 推开。当文本为“半长”时,图像变得很小但仍然可见。我希望它始终为 30x30 像素,文本可以换行并使用另一行。

我尝试指定宽度和最小宽度的值。还为 ImageView 尝试了 layout_weight="1"。还尝试将 ImageView 包装到 LinearLayout 并提供 layout_weight="1" 参数。没有任何效果。

下面是一个不工作的例子:

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

    <LinearLayout android:layout_width="wrap_content"
       android:layout_height="wrap_content" android:orientation="vertical">

       <TextView android:id="@+id/popupTitle" android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>

       <TextView android:id="@+id/popupContent"
      android:layout_width="wrap_content" android:layout_height="wrap_content"/>
    </LinearLayout>

    <ImageView android:layout_width="wrap_content"
       android:layout_height="wrap_content" android:src="@drawable/img_30x30_px" />
 </LinearLayout>

【问题讨论】:

  • RelativeLayout 可能会让你的运气更好
  • ImageView 的最大宽度和高度是多少?如果您可以模拟您期望的结果,那将会很有帮助。

标签: android android-layout width android-imageview


【解决方案1】:

我遇到过类似的问题,我发现 TableView 布局对我有用。这花了一些时间,但您可以使用拉伸和延伸列属性来更改列扩展以匹配内容的行为。

请注意,您应该能够将文本的 linearLayout 设置为 fill_parent(以填充列空间)。

阅读本文了解 TableRow 的工作原理http://developer.android.com/resources/tutorials/views/hello-tablelayout.html

<TableRow>
    <!-- Column 1 -->
    <LinearLayout android:layout_width="fill_parent"
   android:layout_height="wrap_content" android:orientation="vertical">

            <TextView
                android:layout_column="1"
                android:text="Open..."
                android:padding="3dip" />
            <TextView
                android:text="Ctrl-O"
                android:gravity="right"
                android:padding="3dip" />
    </LinearLayout>
    <!-- Column 2 -->
    <ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:src="@drawable/img_30x30_px" />
</TableRow>

(恐怕我不在一台装有 Android 的机器上,所以我无法测试上面的代码)。

【讨论】:

    【解决方案2】:

    非常感谢埃米尔!有用!你让我的周末! :)

    我必须马上尝试(虽然现在是星期五晚上),这就是我的 xml 现在的样子:

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:shrinkColumns="0">  
    
    <TableRow>
    <!-- Column 1 -->
    <LinearLayout android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:orientation="vertical">
    
      <TextView android:id="@+id/popupTitle"
      android:layout_width="wrap_content" android:layout_height="wrap_content"/>
    
      <TextView android:id="@+id/popupContent"
      android:layout_width="wrap_content" android:layout_height="wrap_content" />
      </LinearLayout>
    
      <!-- Column 2 -->
      <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content"
      android:layout_gravity="center_vertical" android:src="@drawable/img_30x30_px" />
    
    </TableRow>
    </TableLayout>
    

    【讨论】:

    • 请将我的答案设为正确答案,以便其他人看到您的问题已得到解答。谢谢
    猜你喜欢
    • 1970-01-01
    • 2016-03-22
    • 2021-01-29
    • 1970-01-01
    • 2011-08-12
    • 2018-02-26
    • 2017-03-01
    • 1970-01-01
    • 2020-09-07
    相关资源
    最近更新 更多