【问题标题】:ExpandableListView indicator is not aligned correctlyExpandableListView 指示器未正确对齐
【发布时间】:2013-10-08 18:54:00
【问题描述】:

我以编程方式创建了一个 ExpandableListView,并且我将 TextView 用于 groupitems。我现在遇到了 groupIndicator 的问题,当在 xml 中声明 ExpandableListView 时可能不会发生该问题:指示器直接绘制到 groupItem 的顶部(“无 paddingTop”)。我在我的 TextViews 上使用填充,所以看起来指示器没有与 TextView 的高度对齐。如何正确对齐指示器?

谢谢

编辑: 在我的自定义 BaseExpandableListAdapter 中:

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    Context c = context.get();
    if(c==null)
        return null;
    if(convertView==null){
        convertView = new TextView(c);
        TextView t = (TextView) convertView;
        t.setText(groupData.get(groupPosition));
        t.setPadding(left, groupPadding, left, groupPadding);  // left '=' android.R.attr.expandableListPreferredItemPaddingLeft
        t.setTextSize(TypedValue.COMPLEX_UNIT_PX, groupTextSize);
        t.setTextColor(Color.WHITE);
        return t;
    }
    TextView t = (TextView) convertView;
    t.setText(groupData.get(groupPosition));
    return t;

}

这是一个未展开的 groupItem 的屏幕截图:

如您所见,可绘制的指示器触及 groupItem 的上边缘。

编辑2: 我设置了一个自定义 Drawable 作为指示器,希望它能解决我的问题。起初我的drawable被拉伸以适应groupItem的高度。我现在使用 9 补丁来防止指标被拉伸,但这会导致指标与底部对齐(而不是像以前那样与顶部对齐)。好吧,同样的问题刚刚逆转。有什么想法可以使用我的自定义可绘制对象解决问题吗?也许有一种方法可以为指示器添加填充(因为我知道可绘制高度和 groupItem 的高度)?

//indicator_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_expanded="true" android:drawable="@drawable/arrow_right" />
    <item android:drawable="@drawable/arrow_down" />

</selector>

//in MainActivity.onCreate()
expView = new ExpandableListView(context);
indicator = context.getResources().getDrawable(R.drawable.indicator_selector);
expView.setGroupIndicator(indicator);
expView.setIndicatorBounds(expView.getPaddingLeft(), expView.getPaddingLeft()+indicator.getIntrinsicWidth());

【问题讨论】:

  • 显示一些代码。截图将是一个不错的奖励。

标签: android expandablelistview


【解决方案1】:

根据这个问题和 coderat 的回答,我想出了如何使用 9patches [ExpandableListView - group indication is stretch to fit the text size] 来解决我的问题:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-02
    • 2017-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多