背景图像在 getView 方法中无法正常工作

发布于 2024-12-27 12:40:12 字数 2659 浏览 5 评论 0原文

以下显示了在 listview 中显示数据的代码和屏幕截图,其中包含 4 个 textview其中仅显示 3 个)和 1 个 图像视图

我在 Adapter 类的 getView() 方法中尝试了以下代码,为交替行(即行号)提供不同的背景图像。 0、2、4等。

当我尝试实现这一点时,我得到的输出如屏幕截图所示,即每当任何 textView 数据较多时,它会换行到第二行,然后文本不会正确显示。

此外,即使 xml 包含 gravity="top",每行顶部仍然留有一些空间。

并且,请参阅下面的 xml,其中使用了 alignTopalignBottom 参数,但第二行仍然没有正确获取背景颜色,因为它的大小相当于 2

注意:即使我使用 COLOR 而不是 IMAGE 作为背景,结果是相同的。所以我想,问题与图像尺寸无关。

getView() 方法的片段

 if (position % 2 == 0) 
     holder.l1.setBackgroundResource(R.drawable.cellbg);
     // l1 is reference of LinearLayout used in below xml.
 else 
     holder.l1.setBackgroundResource(R.drawable.cellbg1);

列表结构(xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    android:id="@+id/tableLayout" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/FirstText"
        android:layout_width="70dp"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:textSize="9sp"/>
    
    <TextView
        android:id="@+id/SecondText"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:layout_alignTop="@+id/FirstText"
        android:layout_alignBottom="@+id/FirstText"
        android:textSize="9sp"/>
    
    <TextView
        android:id="@+id/ThirdText"
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:layout_alignTop="@+id/FirstText"
        android:layout_alignBottom="@+id/FirstText"
        android:textSize="9sp"/>
    
    <TextView
        android:id="@+id/FourthText"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:gravity="top"
        android:layout_alignTop="@+id/FirstText"
        android:layout_alignBottom="@+id/FirstText"/>
    
    <ImageView
        android:id="@+id/ivarrow"
        android:layout_width="15dp"
        android:layout_height="wrap_content" 
        android:layout_alignTop="@+id/FirstText"
        android:layout_alignBottom="@+id/FirstText"
        android:paddingTop="5dp"
        android:gravity="top"/>

</LinearLayout>

我做错了什么?

screen

Following shows the code and screenshot for displaying data in listview containing 4 textview (out of which only 3 will be displayed) and 1 imageView.

I tried the following code in getView() method of Adapter class to give different background image to alternate rows i.e. row no. 0,2,4,etc.

When I try to implement this, I get the output as shown in screen-shot i.e. whenever any textView data is more so that it wraps to 2nd line, then text is not getting displayed appropriately.

Also even though xml contains gravity="top", still some space is getting left out at top in each row.

And, refer to below xml in which alignTop and alignBottom parameters are used, still 2nd row is not getting background color appropriately as it's size is equivalent to 2 lines.

NOTE : Even if I use COLOR instead of IMAGE for background, the result is same. So I guess, issue is not related with image dimensions.

snippet of getView() method

 if (position % 2 == 0) 
     holder.l1.setBackgroundResource(R.drawable.cellbg);
     // l1 is reference of LinearLayout used in below xml.
 else 
     holder.l1.setBackgroundResource(R.drawable.cellbg1);

list structure(xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    android:id="@+id/tableLayout" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/FirstText"
        android:layout_width="70dp"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:textSize="9sp"/>
    
    <TextView
        android:id="@+id/SecondText"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:layout_alignTop="@+id/FirstText"
        android:layout_alignBottom="@+id/FirstText"
        android:textSize="9sp"/>
    
    <TextView
        android:id="@+id/ThirdText"
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:layout_alignTop="@+id/FirstText"
        android:layout_alignBottom="@+id/FirstText"
        android:textSize="9sp"/>
    
    <TextView
        android:id="@+id/FourthText"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:gravity="top"
        android:layout_alignTop="@+id/FirstText"
        android:layout_alignBottom="@+id/FirstText"/>
    
    <ImageView
        android:id="@+id/ivarrow"
        android:layout_width="15dp"
        android:layout_height="wrap_content" 
        android:layout_alignTop="@+id/FirstText"
        android:layout_alignBottom="@+id/FirstText"
        android:paddingTop="5dp"
        android:gravity="top"/>

</LinearLayout>

What am I doing wrong?

screen

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别在捏我脸啦 2025-01-03 12:40:12

重力:垂直中心?
我更喜欢将 TextView 设置为 SingleLine。我认为看起来会更好看
和layout_alignTop和layout_alignBottom仅适用于RelativeLayout
谢谢 :)

gravity:center_vertical?
I prefer make the TextView SingleLine.. I will seems a better look I think
and layout_alignTop layout_alignBottom only work in RelativeLayout
thx :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文