4

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

Bu 该行的显示方式与android:layout_height="wrap_content".

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

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

4

3 回答 3

5

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

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

于 2013-04-14T07:23:15.523 回答
3

只是想知道是否有特定原因为什么要让 ListView 只包含一行?而不是直接使用RelativeLayout而不是ListView?

于 2012-10-25T14:10:53.033 回答
0

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

请让我知道是否有更好的解决方案让 Android 完成它的工作并最小化硬代码。

于 2012-10-25T15:12:24.197 回答