我有一个自定义列表视图 - 其中的对象来自自定义BaseAdapter。我还为滚动条创建了以下形状:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#CC0a2b41"></solid> <corners android:radius="8dp" /> <padding android:top="20dp" android:left="5dp" android:right="5dp"/> </shape>
但是现在滚动条粘在屏幕的右侧,我希望它略微向左,这样滚动条和屏幕边缘之间有一个小填充。无论我放在那里的值是什么,上述形状的填充属性似乎都无能为力。
我的列表视图,如果有帮助:
<ListView android:id="@+id/mainMenuListView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/textView3" android:divider="@color/blue_border" android:dividerHeight="1px" android:scrollbarThumbVertical="@drawable/scroller" >
我在xml中尝试过scrollbarPositionStyles,但没有达到预期的效果 -
答案 0 :(得分:0)
试试这个。 您可以使用View.SCROLLBAR_POSITION_LEFT将任何视图的滚动条位置向左移动。
答案 1 :(得分:0)
你也可以试试这个,至少2.2以上。
import java.lang.reflect.Field;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
public class ListViewWithLeftScrollBar extends ListView {
private static final String LOG_TAG = ListViewWithLeftScrollBar.class.getSimpleName();
private static final boolean DEBUG = true;
private boolean patchInvalidate;
public ListViewWithLeftScrollBar(Context context) {
super(context);
moveVerticalScrollbarToTheLeft();
}
public ListViewWithLeftScrollBar(Context context, AttributeSet attrs) {
super(context, attrs);
moveVerticalScrollbarToTheLeft();
}
public ListViewWithLeftScrollBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
moveVerticalScrollbarToTheLeft();
}
@Override
public void invalidate(Rect r) {
invalidate(r.left, r.top, r.right, r.bottom);
}
@Override
public void invalidate(int left, int top, int right, int bottom) {
int width = right - left;
if (DEBUG) log("invalidate original w:"+ getWidth() +" h:"+ getHeight()+" rect:"+left+", "+top+", "+right+", "+bottom);
if (patchInvalidate && right == getWidth() && top == 0 && bottom == getHeight() && width < 30) {
// The above condition should ensure that ListView is VERY likely to be invalidating the scrollbar.
// In fact ListView appears to not invalidate anything except the scrollbar, ever.
left = 0;
right = left + width;
if (DEBUG) log("invalidate patched w:"+ getWidth() +" h:"+ getHeight()+" rect:"+left+", "+top+", "+right+", "+bottom);
}
super.invalidate(left, top, right, bottom);
}
private void moveVerticalScrollbarToTheLeft() {
try {
if (DEBUG) log("moveVerticalScrollbarToTheLeft: Trying API Level >=11");
tryApiLevel11();
if (DEBUG) log("moveVerticalScrollbarToTheLeft: API Level >=11 success");
} catch (Throwable t1) {
if (DEBUG) {
log("moveVerticalScrollbarToTheLeft: API Level >=11 FAILED");
t1.printStackTrace();
}
try {
if (DEBUG) log("moveVerticalScrollbarToTheLeft: Trying hack for API Level <11");
tryApiLevelPre11();
patchInvalidate = true;
if (DEBUG) log("moveVerticalScrollbarToTheLeft: API Level <11 hack success");
} catch (Throwable t2) {
if (DEBUG) {
log("moveVerticalScrollbarToTheLeft: API Level <11 hack FAILED");
t2.printStackTrace();
}
}
}
}
@SuppressLint("NewApi")
private void tryApiLevel11() {
setVerticalScrollbarPosition(View.SCROLLBAR_POSITION_LEFT);
}
private void tryApiLevelPre11() throws Exception {
Class<?> viewClass = View.class;
Field scrollCacheField = viewClass.getDeclaredField("mScrollCache");
scrollCacheField.setAccessible(true);
Object scrollCache = scrollCacheField.get(this);
if (DEBUG) log("scrollCache: "+ scrollCache);
Field scrollBarField = scrollCache.getClass().getDeclaredField("scrollBar");
scrollBarField.setAccessible(true);
Object scrollBar = scrollBarField.get(scrollCache);
if (DEBUG) log("scrollBar: "+ scrollBar);
Field verticalThumbField = scrollBar.getClass().getDeclaredField("mVerticalThumb");
verticalThumbField.setAccessible(true);
Object verticalThumb = verticalThumbField.get(scrollBar);
if (DEBUG) log("verticalThumb: "+ verticalThumb);
Drawable verticalThumbDrawable = (Drawable) verticalThumb;
Drawable replacementVerticalThumbDrawable = new LayerDrawable(new Drawable[]{ verticalThumbDrawable }) {
@Override
public void setBounds(int left, int top, int right, int bottom) {
if (DEBUG) log("setBounds original: "+left+", "+top+", "+right+", "+bottom);
int width = right - left;
left = 0;
right = left + width;
if (DEBUG) log("setBounds patched: "+left+", "+top+", "+right+", "+bottom);
super.setBounds(left, top, right, bottom);
}
};
verticalThumbField.set(scrollBar, replacementVerticalThumbDrawable);
}
private static void log(String msg) {
Log.d(LOG_TAG, msg);
}
}