You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过代码设置ExpandableListView子布局窄于组头并居中?

How to Make ExpandableListView Child Items Narrower & Centered Than Headers (Code-Only Solution)

Hey there! I’ve tackled this exact issue with ExpandableListView before—when XML tweaks won’t cut it for visual requirements, dynamically adjusting layout params in code is the way to go. Let’s break down how to get those child items narrower than your group headers and perfectly centered:

Step 1: Work Inside Your Adapter’s getChildView() Method

This is where you’ll modify the child item’s layout parameters every time it’s rendered. The key is to calculate the desired width relative to your group header, then apply new layout rules.

First, make sure your child item XML layout has its root view set to android:width="match_parent"—this gives us a base to adjust from. Your group header (category layout) should also be set to match_parent so we use its full width as a reference.

Step 2: Code Implementation Example

Here’s a concrete snippet for your adapter’s getChildView() override:

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    // Inflate your child layout if it's not already recycled
    if (convertView == null) {
        convertView = LayoutInflater.from(mContext).inflate(R.layout.your_child_item_layout, parent, false);
    }

    // Calculate the horizontal offset (adjust this DP value to your visual needs)
    int horizontalOffset = (int) TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP,
        24, // 24dp left/right margin - tweak this!
        mContext.getResources().getDisplayMetrics()
    );

    // Get the full width of the group header (from the parent ExpandableListView)
    int groupHeaderWidth = parent.getWidth();
    // Calculate the child item's target width (header width minus 2x offset)
    int childTargetWidth = groupHeaderWidth - (2 * horizontalOffset);

    // Get and modify the child view's layout params
    ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) convertView.getLayoutParams();
    layoutParams.width = childTargetWidth;
    layoutParams.leftMargin = horizontalOffset;
    layoutParams.rightMargin = horizontalOffset;
    // Ensure the child is centered (redundant with margins, but safe to add)
    convertView.setGravity(Gravity.CENTER_HORIZONTAL);

    // Apply the updated params
    convertView.setLayoutParams(layoutParams);

    // Bind your child data here (text, images, etc.)

    return convertView;
}

Step 3: Handle Edge Cases

  • If parent.getWidth() returns 0: This happens if the ExpandableListView hasn’t finished measuring yet. Fix this by adding a ViewTreeObserver to wait for the parent to be measured:

    parent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            // Remove the listener to avoid repeated calls
            parent.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            // Refresh the adapter to apply the correct widths
            notifyDataSetChanged();
        }
    });
    

    Add this code in your adapter’s constructor or when you initialize the ExpandableListView.

  • If using ConstraintLayout for child items: Swap ViewGroup.MarginLayoutParams with ConstraintLayout.LayoutParams, then set width and adjust horizontalBias or left/rightMargin as needed.

Quick Checks

  • Double-check your group header layout is set to match_parent so we’re using the correct full width.
  • Avoid setting fixed widths in your child XML—let the code handle that.

内容的提问来源于stack exchange,提问作者MIKE PERETZ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:19:59