【问题标题】:Android: how to change the position of ExpandableListView Indicator?Android:如何改变 ExpandableListView Indicator 的位置?
【发布时间】:2011-07-05 04:46:13
【问题描述】:

我想更改 ExpandableListView 的 Group 视图中显示的默认箭头的位置。我希望它在右边而不是在左边。

如何做到这一点?

【问题讨论】:

    标签: android listview


    【解决方案1】:

    其实有一个简单的方法。

    1. 像这样编辑与组视图相关的 XML:

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginRight="35dp"
      android:orientation="horizontal" >
      
      <TextView
          android:id="@+id/videos_explist_groupname"
          android:layout_width="match_parent"
          android:layout_height="?android:attr/listPreferredItemHeight"
          android:layout_alignParentLeft="true"
          android:layout_alignParentTop="true"
          android:layout_toLeftOf="@+id/videos_group_indicator"
          android:gravity="center"
          android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
          android:textAppearance="?android:attr/textAppearanceListItem" />
      
      <ImageView
          android:id="@+id/videos_group_indicator"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentRight="true"
          android:layout_centerVertical="true"
          android:src="@drawable/videos_chevron_collapsed" />
      
      </RelativeLayout>
      

      查看ImageView 位于TextView 右侧的情况。

    2. 如下编辑您的ExpandableListAdapter

      @Override
      public View getGroupView(final int groupPosition, final boolean isExpanded, final View convertView, final ViewGroup parent) {
          View v;
          if (convertView == null) {
              v = newGroupView(isExpanded, parent);
          } else {
              v = convertView;
          }
          bindView(v, mGroupData.get(groupPosition), mGroupFrom, mGroupTo);
          ((ImageView) v.findViewById(R.id.videos_group_indicator))
              .setImageResource(isExpanded?R.drawable.videos_chevron_expanded:R.drawable.videos_chevron_collapsed);
          return v;
      }
      

      如您所见,您可以轻松设置与组状态对应的可绘制对象。

    我知道这个问题是很久以前提出的,但这可能会对某人有所帮助。

    【讨论】:

    【解决方案2】:

    使用setIndicatorBounds:

    设置指标的绘制边界(至少,组 指标受此影响;子指标受此影响 如果子指示器边界设置为继承)。

    【讨论】:

      【解决方案3】:

      试试这个

      expList = getExpandableListView();
      metrics = new DisplayMetrics();
      getWindowManager().getDefaultDisplay().getMetrics(metrics);
      width = metrics.widthPixels;
      //this code for adjusting the group indicator into right side of the view
      expList.setIndicatorBounds(width - GetDipsFromPixel(50), width - GetDipsFromPixel(10));
      

      GetDipsFromPixel 在这里

      public int GetDipsFromPixel(float pixels)
      {
         // Get the screen's density scale
         final float scale = getResources().getDisplayMetrics().density;
         // Convert the dps to pixels, based on density scale
         return (int) (pixels * scale + 0.5f);
      }
      

      【讨论】:

        【解决方案4】:

        如果您想将位置指示器从左侧更改为右侧,您需要添加android:layoutDirection= "rtl"。但是列表的位置会变到右边。

        示例代码如下所示

        <ExpandableListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="none"
            android:layoutDirection="rtl"
            />
        

        【讨论】:

          【解决方案5】:

          没有直接的方法可以做到这一点。

          mIndicatorLeft 和 mIndicatorRight 是 ExpandableListView 类中处理指标图标位置的两个字段,它们是私有字段。不幸的是,指标的位置没有像预期的那样在定义整齐的布局中设置,而是在 ExpandableListView 中的 dispatchDraw() 上确定的。使用 top/bottom 和 mIndicatorRight 和 mIndicatorLeft 值绘制一个矩形,在此 Rect 中绘制指标图标。您必须更改此名为“indicatorRect”的 Rect obj 的位置才能移动您的指示器图标。

          要了解一些事情,您可以查看此示例 http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList1.html 将例子中getGenericView()中textView padding的值改为0,你会看到指示器会和文本重叠。

          您唯一的选择是获取 ExpandableListView 的源代码并自己设置这些 mIndicatorRight 和 mIndicatorLeft 值,方法是将 mIndicatorRight 设置为 item.getRight() 并将 mIndicatorLeft 设置为 item.getRight() - '您的图标大小'。

          【讨论】:

            猜你喜欢
            • 2021-08-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多