根据其他View的宽度设置TextView宽度

时间:2013-08-08 08:08:45

标签: android android-layout

我有ImageView,图片是从网上加载的,宽度可能会有所不同。 下面我有TextView,其中包含图片说明。我希望TextViewImageView完全一样宽。

我的尝试是这样的:

        <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在2行后结束:

enter image description here

我知道在知道图像的宽度后,我可以以编程方式设置TextView的宽度,但是还有一种方法可以在XML布局中进行吗?

7 个答案:

答案 0 :(得分:5)

我不知道它是否可行,但在加载图片后尝试使用代码:

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

答案 1 :(得分:4)

只需稍加更改布局:将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

对齐

答案 2 :(得分:1)

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" />

答案 3 :(得分:0)

您可以在xml中使用wight属性,并为imageview和textview赋予相同的权重。然后两个大小都相等

答案 4 :(得分:0)

在你的情况下这样做:

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

答案 5 :(得分:0)

这对我有用:

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

enter image description here enter image description here

答案 6 :(得分:0)

试试这个。这会有效吗

  <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>
相关问题