在Android ListView
中,我怎樣才能讓scrollbar
出現在左側?如何讓`scrollbar`出現在左側?
回答
例如:
mView.setVerticalScrollbarPosition(View.SCROLLBAR_POSITION_LEFT);
您可以使用View.SCROLLBAR_POSITION_LEFT將滾動條位置向左移動以進行任何視圖。
作爲其它兩個答案都提到,一種可能性是使用View.setVerticalScrollbarPosition()SCROLLBAR_POSITION_LEFT用。然而,一個巨大的警告是,這需要API級別11+,在寫入Android安裝的時間不到10%的時候。對於大多數應用程序,這是不可接受的。
我想到的來完成你想要的東西在舊版本的Android的是做一件很缺憾一種可能性:關閉滾動條,用窄佈局鏡像您的主要佈局於它的左側,稍稍偏出足以適合滾動條,並在您的主視圖滾動時通過手動滾動左側視圖scrollyBy()(通過覆蓋 onScrollChanged())。
這就是說,我不會真正推薦,除非有非常令人信服的理由將滾動條移動到左側。在大多數情況下,只要讓Android遵循其默認設置,即可讓您的應用適應設備上的其他應用,並像其他應用一樣運行。
試試我的黑客,似乎至少在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;
/**
* This class fixes the lack of setVerticalScrollbarPosition(View.SCROLLBAR_POSITION_LEFT) before API level 11
* @author Genadz Batsyan
*/
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);
}
}
我在2.2.1上試過了,它沒有工作。 –
好吧,如果我在'if(Build.VERSION.SDK_INT
- 1. 如何讓CheckBoxList中的CheckBox出現在我的文本左側?
- 2. 如何讓Android SlidingDrawer從左側滑出?
- 3. 如何讓jQuery的表演方法,從右側比左側出現
- 4. 如何使子菜單出現在左側,而不是右側
- 5. 如何更改子菜單側出現在左側而不是右側
- 6. 你如何讓左側的菜單保持在右側滾動
- 7. 空間出現在滾動視圖android的右側和左側?
- 8. 如何在Panel上出現ScrollBar時發現? VB.Net
- 9. 我如何讓一個div從右側滑入左側?
- 10. 如何讓自側欄固定到自舉的左側3.1
- 11. 如何讓新的文檔選項卡出現在右側?
- 12. CSS:如何讓標籤出現在收音機的右側?
- 13. 如何讓側欄首先出現在moble中?
- 14. CSS - 如何使溢出隱藏在右側,但不是左側?
- 15. 如何表現出左側引導標籤
- 16. 如何切斷溢出左側
- 17. 如何讓對話框出現在左邊?
- 18. 如何使C#中從右到左的數字左側出現負號?
- 19. 如何讓圖像從左側0,0位置向右側移動到右側?
- 20. 如何在ios的uitextfield的左側添加左側邊距?
- 21. 如何讓jQuery UI Datepicker顯示在輸入字段的左側?
- 22. 在C#中,如何讓datagridview的左側邊框消失?
- 23. 如何讓菜單在WPF左側打開?
- 24. CSS左側沒有出現在屏幕中間:50%左右:左側可見值的一半
- 25. 溢出到左側而不是右側
- 26. 如何讓對象/工具出現在ZMI左側導航框架中的Plone下面?
- 27. 如何對齊右側和左側
- 28. accessoryType在左側
- 29. UL在左側
- 30. 文本溢出到左側
它也可以通過XML:機器人:verticalScrollbarPosition = 「左」 –
遺憾的是不能在[NavigationView]工作(http://developer.android.com/intl/ru/reference/android/support/design/ widget/NavigationView.html)(API 23)。 – Luten