我在这个布局中缺少什么

时间:2013-11-26 21:23:45

标签: android android-layout

我想为listview的每一行实现这样的布局

enter image description here

所以我创建了一个像这样的容器

<?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="wrap_content"
              android:orientation="horizontal"
              android:clickable="false"
              android:focusable="false"
        >

    <!--List items goes here -->
</LinearLayout>

在我的自定义适配器的getView()方法中,我为上面图像中的每个框充气这个xml

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
                 android:layout_width="300dp"
                 android:layout_height="500dp"
                 android:clickable="true"
                 android:focusable="true"
                 android:padding="10dp"
                 android:background="@drawable/list_item_background"
                 android:focusableInTouchMode="true">
    <ImageView
                    android:id="@+id/thumbnail"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scaleType="fitXY"
                    android:src="@drawable/cnbc_default_image"/>

            <ImageView
                    android:id="@+id/supportImageView"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:visibility="gone"
                    android:src="@drawable/video_play_icon"
                    android:paddingLeft="10dp"
                    android:layout_above="@+id/title"/>

            <TextView
                    android:id="@id/title"
                    android:layout_height="wrap_content"
                    android:layout_width="match_parent"
                    android:layout_above="@+id/description"
                    style="@style/large_news_title_text_style"/>

            <TextView android:id="@id/description"
                      android:layout_height="wrap_content"
                      android:layout_width="match_parent"
                      android:layout_alignParentBottom="true"
                      android:maxLines="2"
                      android:ellipsize="end"
                      android:textSize="13sp"
                      style="@style/large_news_title_text_style"
                      android:textColor="@android:color/white" />

</RelativeLayout>

然后最后使用

将膨胀的视图添加到linearlayout实例

row.addView(inflatedView);

然而,最终结果是第一个框横跨X轴拉伸。如果我使用HierachyViewer检查我的布局,我确实在LinearLayout中看到3个RelativeLayouts,但第一个占据了所有空间。

缩略图android:id =&#34; @ + id / thumbnail&#34;,似乎占用了LinearLayout的match_parent属性,而不是它的直接容器RelativeLayout。

澄清

列表视图来自ListFragment。 将框布局添加到列表视图行容器时,我使用此方法调用

ViewGroup container = (ViewGroup)inflater.inflate(R.layout.list_view_row_land, parent, false);

View rowItem = inflater.inflate(R.layout.list_view_row_item,null);

container.addView(rowItem);

解决: 如果我将框的布局修改为

,问题就解决了
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
                 android:layout_width="500dp"
                 android:layout_height="300dp"
                 android:clickable="true"
                 android:focusable="true"
                 android:padding="10dp"
                 android:background="@drawable/list_item_background"
                 android:focusableInTouchMode="true">
    <ImageView
                    android:id="@+id/thumbnail"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scaleType="fitXY"
                    android:src="@drawable/cnbc_default_image"/>

            <ImageView
                    android:id="@+id/supportImageView"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:visibility="gone"
                    android:src="@drawable/video_play_icon"
                    android:paddingLeft="10dp"
                    android:layout_above="@+id/title"/>

            <TextView
                    android:id="@id/title"
                    android:layout_height="wrap_content"
                    android:layout_width="match_parent"
                    android:layout_above="@+id/description"
                    style="@style/large_news_title_text_style"/>

            <TextView android:id="@id/description"
                      android:layout_height="wrap_content"
                      android:layout_width="match_parent"
                      android:layout_alignParentBottom="true"
                      android:maxLines="2"
                      android:ellipsize="end"
                      android:textSize="13sp"
                      style="@style/large_news_title_text_style"
                      android:textColor="@android:color/white" />

</RelativeLayout>
</LinearLayout>

我仍然不明白为什么。因此,解释为什么将LinearLayout放在一起解决问题的任何解释都会有所帮助。

0 个答案:

没有答案
相关问题