【发布时间】:2014-05-17 07:10:13
【问题描述】:
Except for creating your own layout,有什么办法可以将组指示器从左侧移动到右侧?
我想知道 Android 有这样的属性来处理这个吗?
另外,android:indicatorStart|End|Left|Right 到底是做什么的?看了文档还是不明白。
【问题讨论】:
标签: android android-layout expandablelistview
Except for creating your own layout,有什么办法可以将组指示器从左侧移动到右侧?
我想知道 Android 有这样的属性来处理这个吗?
另外,android:indicatorStart|End|Left|Right 到底是做什么的?看了文档还是不明白。
【问题讨论】:
标签: android android-layout expandablelistview
首先有 2 种移动组图标指示器的方法,具体取决于设备 android 版本,因此对于 sdk 版本 18 及更高版本(android 4.3 及更高版本)的设备,有一种称为 setIndicatorBoundsRelative(left,right) 的方法,对于较低版本有另一种方法称为setIndicatorBounds(left,right)。上述两个函数都会更改组指标的边界,其中第一个参数将设置指标的开始位置,而右将设置指标的结束位置。
代码:
在声明和初始化expandablelist的activity中,声明如下为类变量(expandableListView的适配器和视图除外)
DisplayMetrics diaplayMetrics;
int width;
在on create函数中:
@Override
onCreate(Bundle savedInstanceState) {
//initialze displayMetrics
metrics = new DisplayMetrics();
//get the metrics of window
getWindowManager().getDefaultDisplay().getMetrics(metrics);
save width of window
width = metrics.widthPixels;
//check version of sdk(android version)
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
//For sdk version bellow 18
expandableListView.setIndicatorBounds(width - GetDipsFromPixel(50), width - GetDipsFromPixel(10));
} else {
//For sdk 18 and above
expandableListView.setIndicatorBoundsRelative(width - GetDipsFromPixel(50), width - GetDipsFromPixel(10));
}
expandableListView.setAdapter(listAdapter);
}
private int GetDipsFromPixel(int 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);
}
【讨论】: