【问题标题】:Single row to fill_parent ListView height单行到fill_parent ListView 高度
【发布时间】:2012-10-25 14:03:01
【问题描述】:

listView 中有一行我希望与 listView 具有相同的高度(假设是全屏)。

行布局看起来像

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

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/error" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="14dp"
        android:minHeight="30dip"
        android:textAppearance="?android:attr/textAppearanceMedium"/>
</RelativeLayout>

而适配器的getView是

@Override
public View getView(int position, View convertView, ViewGroup parent) {
   View row = inflater.inflate(R.layout.myrow, parent, false);
   return row;
}

但该行的显示方式与 android:layout_height="wrap_content" 相同。

布局预览显示填充它的父行,我正在使用inflate(R.layout.myrow, parent, false);,listView 肯定会全屏显示,并且该行仅与 image + textView 一样高。

我错过了什么重要的东西吗?

【问题讨论】:

  • RelativeLayout 不需要 "android:orientation="vertical"",而 LinearLayouts 则需要。用户“match_parent”而不是“fill_parent”并尝试在 myrow.xml 中为您的根 RelativeLayout 设置一些背景,也许该行具有全屏宽度,问题在于将 Image 和 TextView 放置在 listview 布局中?
  • @fgeorgiew 我知道,它是由 LinearLayout 留下的,我相信被忽略没有什么坏处。

标签: android android-listview


【解决方案1】:

我有类似的问题,我想我已经解决了,所以在这里报告。

我的 ListView 中有更多行,但我希望行高与 listview 相同,这反过来又会占用布局的所有剩余空间。我的行仅由一个 ImageView 组成。我遇到了以下问题:

-我希望较小的图像扩展以占据所有空间 - 更大的图像不应超过行高

如果我有任何错误,请在此处留言。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.9"
    android:gravity="center"
    android:orientation="vertical">
    <ListView 
        android:id="@id/lay_main_firma_objekti"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">
    </ListView>      
</LinearLayout>   

行布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:gravity="center">       
  <TextView
      android:id="@+id/xx"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>  
    <ImageView              
        android:id="@id/imageView_obj"
        android:contentDescription="@string/objektDesc"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignLeft="@id/xx"
        android:layout_alignTop="@id/xx"
        android:layout_alignRight="@id/xx"
        android:layout_alignBottom="@id/xx"
        android:scaleType="fitXY"
        android:src="@drawable/custom_prazno" />                    
</RelativeLayout>

那么在Adapter getView中就很重要了

convertView.setMinimumHeight(parent.getMeasuredHeight());
ImageView slika=(ImageView)v.findViewById(R.id.imageView_obj);    
slika.setMinimumHeight(parent.getMeasuredHeight());
TextView xx=(TextView)v.findViewById(R.id.xx);
xx.setHeight(parent.getMeasuredHeight());

我认为是的。 hth

【讨论】:

  • 谢谢,getMeasuredHeight 是最小化硬代码的好方法,尽管我不确定在 listView 调整大小期间是否为显示的行调用 getView
【解决方案2】:

只是想知道您是否有特定的原因希望 ListView 只包含一行?而不是直接使用RelativeLayout而不是ListView?

【讨论】:

  • 是的,有一个强有力的理由,listView 用于显示表格内容但有时我需要显示错误消息,因此我决定为此使用特殊行。它非常适合 listView pull-to-refresh 功能,我不想将 listView 数据源链接到 listView 以外的任何其他视图,这让事情变得太复杂了。
【解决方案3】:

我最终对行高进行了硬编码。设置 minimumHeight 后,相对布局出现问题,因此我也将其替换为具有中心重心的 LinearLayout。

如果有更好的解决方案让 Android 来完成它的工作并最大限度地减少硬代码,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-01
    • 2012-09-16
    • 2012-07-31
    • 1970-01-01
    • 2011-02-13
    • 2013-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多