【问题标题】:How to change the background color of an ExpandableListView in Android?如何在 Android 中更改 ExpandableListView 的背景颜色?
【发布时间】:2014-02-13 07:50:30
【问题描述】:

我的问题的标题几乎概括了我想问的问题,所以在描述中我将详细描述我的问题。

所以我有一个 ExpandableListView,它以黑色打印在屏幕上,里面有白色文本。当我按下列表时,它会被消耗,我可以选择其中的详细信息。它看起来像这样:

|--------------------------|
|black color               | 
|   Title text(in white)   |
|                          |
|__________________________|
|  Item 1  (black text with white background)
|  Item 2                  |
|  Item N...               |

所以我的问题是如何更改“文本标题”内部的背景颜色并限制标题中显示的符号数量?

这是我的 XML 文件:

    <ScrollView
    android:layout_width="fill_parent"
    android:layout_height="228dp" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#f4f4f4"
        android:orientation="vertical" >

        <ExpandableListView
            android:id="@+id/lvExp"
            android:layout_width="match_parent"
            android:layout_height="485dp"          >
        </ExpandableListView>
    </LinearLayout>
    </ScrollView>

项目。 XML:

<?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="55dip"
android:orientation="vertical" >

 <TextView
    android:id="@+id/lblListItem"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="17dip"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"        
    android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />


</LinearLayout>    

组.XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp"
android:background="#000000">


<TextView
    android:id="@+id/lblListHeader"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
    android:textSize="17dp"        
    android:textColor="#f4f4f4" 
    android:background="#323232"/>//this only changes some part of the background just around the text ONLY

</LinearLayout>

【问题讨论】:

  • “限制标题中显示的符号数量”是什么意思?
  • 例如如果标题是“我爱吃肉”在屏幕上会出现“我爱吃...”
  • 为 textview 添加 android:singleLine="true" 属性
  • 要更改背景颜色,为布局设置背景颜色,而不是为 textview 设置背景颜色
  • 很好,这确实起到了改变背景颜色的作用!谢谢!但是符号的数量还是一样的:/

标签: android xml background expandablelistview


【解决方案1】:

在 textview 中试试这些属性:

<TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:maxLines="1" 
    android:maxLength="10" 
    android:ellipsize="marquee"/>

【讨论】:

  • 非常感谢,最后一个问题是可以在减少字符数后添加“...”吗?
  • 好吧,您正在设置限制,如果字符数不超过该限制,它将打印整个文本。您可以通过覆盖 java 中的 onTextChanged 事件并在文本末尾附加“...”来做到这一点
【解决方案2】:

在可扩展列表适配器中: 在

下的 getGroupView
  if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.listheader, null);
        }

:放这个:

 convertView.setBackgroundColor(Color.parseColor("#FFFFFF"));

并限制在 XML 中使用的文本标题字符:

<TextView 
android:id="@+id/secondLineTextView" 
android:layout_width="wrap_content"
android:layout_height="wrap_content" 
android:maxLines="1" 
android:maxLength="10" 
android:ellipsize="end"/>

【讨论】:

    猜你喜欢
    • 2011-08-06
    • 2013-11-16
    • 2014-01-13
    • 1970-01-01
    • 2019-11-14
    • 1970-01-01
    • 2013-09-12
    • 2014-11-08
    • 1970-01-01
    相关资源
    最近更新 更多