背景图像在 getView 方法中无法正常工作
以下显示了在 listview
中显示数据的代码和屏幕截图,其中包含 4 个 textview
(其中仅显示 3 个)和 1 个 图像视图
。
我在 Adapter 类的 getView() 方法中尝试了以下代码,为交替行(即行号)提供不同的背景图像。 0、2、4等。
当我尝试实现这一点时,我得到的输出如屏幕截图所示,即每当任何 textView
数据较多时,它会换行到第二行,然后文本不会正确显示。
此外,即使 xml
包含 gravity="top"
,每行顶部仍然留有一些空间。
并且,请参阅下面的 xml
,其中使用了 alignTop
和 alignBottom
参数,但第二行仍然没有正确获取背景颜色,因为它的大小相当于 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>
我做错了什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
重力:垂直中心?
我更喜欢将 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 :)