2012-11-29 54 views
0

我試圖使用android:scrollbarThumbVertical="@drawable/scrollbar"參數來添加圖像作爲滾動條。滾動條已更改,但問題是,它不會顯示原始大小,它取決於列表視圖大小(項目)。如果列表項數量爲20意味着它顯示了正確的尺寸,它約爲50意味着,滾動條在整個視圖中拉伸。有沒有辦法避免拉伸。在這裏,我將使用滾動條圖像添加我的代碼以及我的輸出。如何避免Android中的滾動條拉伸列表視圖

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#234232" 
    android:orientation="vertical" > 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="@string/hello" 
     android:textSize="20sp" /> 

    <ListView 
     android:id="@+id/listView1" 
     android:layout_width="250dp" 
     android:layout_height="fill_parent" 
     android:fadeScrollbars="false" 
     android:fadingEdge="none" 
     android:scrollbarSize="12dip" 
     android:scrollbarStyle="outsideInset" 
     android:scrollbarThumbVertical="@drawable/scrollbar" > 
    </ListView> 

</LinearLayout> 

CustomScrollBarAndroidActivity.java

public class CustomScrollBarAndroidActivity extends Activity { 
    private ListView list; 
    private static List<String> listItems = initListItems(); 


    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     list = (ListView) findViewById(R.id.listView1); 
     list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems)); 
    } 


    private static List<String> initListItems() { 
     List<String> list = new ArrayList<String>(); 
     boolean isNotFinish = true; 
     int i = 0; 
     while(isNotFinish){ 
      list.add("list item #" + i); 
      if (i > 50){ 
       isNotFinish = false; 
      } 
      i++; 
     } 
     return list; 
    } 
} 

My output image is

我的滾動條圖像enter image description here

+0

爲什麼不在圖像中間使用帶有拉伸區域的九個補丁圖像? – Luksprog

+0

@Luksprog:對不起,我不知道如何使用九個補丁圖像。你可以plz指導我 – Aerrow

回答

2

對此圖像使用draw9補丁:)

+0

這個鏈接將幫助你(http://developer.android.com/tools/help/draw9patch.html) –

+0

我厭倦了這個教程,黑色的線被繪製在圖像周圍,但沒有什麼會發生滾動條。它仍然是相同的。 – Aerrow

+0

這是避免這種情況的唯一方法。但你必須正確理解本教程,因爲在錯誤的地方畫線不會解決問題。 –