简体   繁体   English

在布局中拟合ImageView和TextView

[英]Fitting ImageView and TextView in a layout

I have horizontal ListView with with following item: 我有水平ListView与以下项目:

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

    <ImageView
        android:id="@+id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:src="@drawable/a" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="Title"
        android:textColor="#FFFFFF" />

</LinearLayout>

This is basically thumbnail and some text beneath it. 这基本上是缩略图,下面是一些文字。 The problem is - when this item is displayed in ListView the text at bottom is only partially visible - bottom part disappears. 问题是 - 当此项目在ListView中显示时,底部的文本仅部分可见 - 底部消失。 Looks like ImageView is not scaling to fit TextView. 看起来ImageView没有缩放以适应TextView。

But if you put TextView above ImageView everything looks fine - ImageView is scaled down and text fits perfectly. 但是如果你把TextView放在ImageView上面,一切看起来都很好 - ImageView缩小了,文字非常合适。 So..why? 所以为什么?

Let me illustrate: 让我说明一下:

TextView at bottom TextView在底部

TextView在底部

TextView at top (images scaled perfectly) 顶部的TextView(图像完美缩放)

TextView在顶部

And as requested main.xml: 并按要求main.xml:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:weightSum="100" >

    <LinearLayout
        android:id="@+id/linearLayout0"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="10"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Top Activity Header"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="90"
        android:orientation="vertical"
        android:weightSum="100" >

        <LinearLayout
            android:background="#6056CC"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="30"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Top"
                android:textAppearance="?android:attr/textAppearanceLarge" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="40" >

            <test.name.Coverflow
                android:id="@+id/coverflow"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent" />
        </LinearLayout>

        <LinearLayout
            android:background="#BF5E76"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="30"
            android:orientation="vertical" >

        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_height="65dp"
        android:id="@+id/linearLayout2"
        style="@style/TabPanel">

    </LinearLayout>

</LinearLayout>

So hey I had this same problem and this is what I did to get it work: 所以嘿我有同样的问题,这就是我做的工作:

<LinearLayout
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:orientation="vertical" 
            android:layout_gravity="center|center_vertical">

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="0dip"
                android:layout_weight="2"
                android:adjustViewBounds="true"
                android:scaleType="fitEnd"
                android:layout_gravity="center|center_vertical"
                android:src="@drawable/ic_pen" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="0dip"
                android:layout_weight="1"
                android:layout_gravity="center|center_vertical"
                android:text="@string/main_write"
                android:textColor="@color/color_darkgray"
                android:textSize="24sp" />
        </LinearLayout>

The trick is to set the TextView and the ImageView's layout_height to "0dip". 诀窍是将TextView和ImageView的layout_height设置为“0dip”。 Set the ImageView's layout_weight to "2" and the TextView's layout_weight to "1". 将ImageView的layout_weight设置为“2”,将TextView的layout_weight设置为“1”。 Set the ImageView's scaleType to "fitEnd". 将ImageView的scaleType设置为“fitEnd”。 Set adjustViewBounds to "true" et voila! 将adjustViewBounds设置为“true”等瞧! Even works when you size up and down so you don't lose the TextView and only the image scales down. 即使在您上下调整大小时也能正常工作,因此您不会丢失TextView,只会缩小图像。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/white"
    android:gravity="center_vertical" >

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical" >

        <TableRow
            android:layout_gravity="left"
            android:padding="5dp" >

            <ImageView
                android:id="@+id/thumbnail"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:src="@drawable/a" 
                android:layout_gravity="left">
            </ImageView>
        </TableRow>

        <TableRow
            android:gravity="left"
            android:padding="5dp" >

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:textColor="@android:color/black"
                />
        </TableRow>
    </TableLayout>

</LinearLayout>

Please try this. 请试试这个。 Hope this would help. 希望这会有所帮助。

You can use relative layout for this.it may helpfull for you. 您可以使用相对布局。这可能对您有所帮助。 The below code 以下代码

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

   <ImageView
     android:id="@+id/thumbnail"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:paddingLeft="5dp"
     android:paddingRight="5dp"
     android:src="@drawable/a" />

   <TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:text="Title"
    android:textColor="#FFFFFF" 
    android:layout_below="@id/thumbnail"/>

    </RelativeLayout>

By using relativelayout, you can set textview below the imageview so that imageview and textview can wrap. 通过使用relativelayout,您可以在imageview下面设置textview,以便imageview和textview可以包装。

Edit: 编辑:

  <RelativeLayout>
       <LinearLayout
         android:id="@+id/linearlayout1">
          //here the textview and imageview code
       </LinearLayout>
      <LinearLayout
       android:layout_below="@id/linearlayout1"
       >
      //here the redblock code
     </LinearLayout>

so now entire redblock will come below the previous layout..may b helpful 所以现在整个redblock都会低于之前的布局..可能会有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM