【问题标题】:Set TextView width according to other View's width根据其他View的宽度设置TextView的宽度
【发布时间】:2013-08-13 18:00:57
【问题描述】:

我有一个ImageView,图片是从网上加载的,宽度可能不同。 下面我有一个带有图像描述的TextView。我希望TextView 与ImageView 一样宽。

我的尝试是这样的:

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

            <ImageView
                android:id="@+id/iv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal" />

            <TextView
                android:id="@+id/imagedescription"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:textAppearance="@style/SmallTextGray" />
        </LinearLayout>

它以某种方式起作用,因为TextView 的宽度是根据ImageView 的宽度设置的,但TextView 在两行之后结束:

我知道我可以在知道图像的宽度后以编程方式设置TextView 的宽度,但是还有没有办法在 XML 布局中做到这一点?

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    我不知道它是否会起作用,但请在加载图像后尝试在您的代码中执行:

    textView.setWidth(imageView.getDrawable().getIntrinsicWidth());
    

    【讨论】:

      【解决方案2】:

      稍微改变布局:将LinearLayout 替换为RelativeLayout:

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" >
      
          <ImageView
              android:id="@+id/iv"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentTop="true"
              android:layout_centerHorizontal="true" />
      
          <TextView
              android:id="@+id/imagedescription"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_alignLeft="@+id/iv"
              android:layout_alignRight="@+id/iv"
              android:layout_below="@+id/iv"
              android:layout_marginTop="8dp"
              android:gravity="center_horizontal"
              android:textAppearance="@style/SmallTextGray" />
      
      </RelativeLayout>
      

      这样,TextView 总是在 imageView 之下,并且它的边界与 imageViews 对齐

      【讨论】:

        【解决方案3】:
        You can use weight like this:
        
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:weightSum="5">
        
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="1" />
        
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:text="2" />
        
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="3" />
        

        【讨论】:

          【解决方案4】:

          您可以在 xml 中使用 wight 属性并赋予 imageview 和 textview 相同的权重。那么两个大小将相等

          【讨论】:

          • @Rushabh Patel .. 我无法在 cmets 中添加代码,因为 commnet 的字符有限。所以只添加为单独的答案。顺便说一句,我不认为通过写多个答案,我们会获得更多的声誉。只是想分享知识和帮助别人..
          • 如果您知道如何一次又一次地编辑答案,那么您可以将所有建议写入您的一个答案。
          【解决方案5】:

          在你的情况下这样做:

                      <ImageView
                          android:id="@+id/iv"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_weight="1"
                          android:layout_gravity="center_horizontal" />
          
                      <TextView
                          android:id="@+id/imagedescription"
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:layout_gravity="center_horizontal"
                          android:layout_weight="1"
          
                          android:textAppearance="@style/SmallTextGray" />
                  </LinearLayout>
          

          【讨论】:

          • 这仅适用于水平线性布局或垂直线性布局中的高度。不过感谢您的想法。
          • 我认为您只使用垂直线性布局。无论如何,你得到了解决方案很好..
          【解决方案6】:

          试试这个。这会奏效

            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center_horizontal"
            android:orientation="vertical" >
          
             <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="fill_parent" 
                 android:layout_gravity="center_horizontal"       
                android:orientation="vertical" >
          
                <ImageView
                    android:id="@+id/iv"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    />
          
                 <TextView
                    android:id="@+id/imagedescription"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_horizontal"
                    android:typeface="monospace" />
                  </LinearLayout>
          
                </LinearLayout>
          

          【讨论】:

            【解决方案7】:

            这对我有用:

            <?xml version="1.0" encoding="utf-8"?>
            
            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                          android:layout_width="match_parent"
                          android:layout_height="match_parent"
                          android:layout_gravity="center_horizontal"
                          android:orientation="vertical">
            
                <LinearLayout android:layout_width="wrap_content"
                              android:layout_height="wrap_content"
                              android:layout_gravity="center_horizontal"
                              android:orientation="vertical">
                    <ImageView
                            android:id="@+id/iv"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/controller"
                            android:layout_gravity="center_horizontal"/>
            
                    <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content">
            
                        <TableLayout
                                android:layout_width="match_parent"
                                android:layout_height="match_parent">
                            <TableRow>
                                <TextView
                                        android:layout_width="wrap_content"
                                        android:text="asdasdasasasdasdasdasdasdasdasdadasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdsadasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdsdasdasdasdsadsadsadasdasdsadsadsadasdasdasdasdasdasddasdasdasdasdasdasdasdsadasdasdasdasdasdasdsadasdasdsa..."/>
                            </TableRow>
                        </TableLayout>
                    </RelativeLayout>
                </LinearLayout>
            
            </LinearLayout>
            

            【讨论】:

            • TextView 的宽度似乎没有与图像对齐,是吗?无论如何,谢谢,我想我会根据 Renan 的回答以编程方式完成。
            • @Ascorbin 抱歉,忘了这是要求,更新了我的答案:)
            猜你喜欢
            • 1970-01-01
            • 2011-09-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-08-27
            相关资源
            最近更新 更多