2013-10-09 55 views
0

我想允許我的ListView被打包到其寬度的內容並使其與父中心對齊。Listview動態分配寬度

這是一個正常的ListView是給我

這幾乎是我想要達到的,只是我想通過的ListView ListView中最長的測試包。目前,這是通過聲明DP這是我想避免實現可能的話

有的爲第一圖像代碼:

<ListView 
      android:id="@+id/lst_test" 
      android:layout_width="0dip" 
      android:layout_weight="1" 
      android:layout_height="wrap_content" > 
     </ListView> 

的第二圖像部分代碼:

<ListView 
    android:id="@+id/lst_test" 
    android:layout_width="200dp" 
    android:layout_height="wrap_content" > 
</ListView> 

我必須使用自定義適配器嗎?或者我可以堅持使用當前的適配器?如果我需要使用自定義適配器,我該如何去做這件事?

+0

請參考http://stackoverflow.com/questions/9483788/center-text-in-a-listview – Azi

+0

發佈您的佈局,其中包含ListView –

回答

1

您不需要創建自定義適配器。這是一個可以幫助你的例子。

public class MainActivity extends Activity { 

    HashMap <String, CheckBox> options = new HashMap<String, CheckBox>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     ListView list_view = (ListView) findViewById(R.id.listView1); 

     ArrayList<String> str = new ArrayList<String>(); 
     for (int i = 0; i < 10; i++) { 
      str.add("Example " + i); 
     } 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_layout, R.id.textView1, str); 
     list_view.setAdapter(adapter); 
    } 
} 

上面我創建了一個活動,其中我使用ArrayAdapter獲取listview和設置數據。

我創建了兩種佈局:

第一個佈局:包含listview。 第二個佈局:包含需要顯示的textview。

對齊Listview中心數據的主要邏輯是需要將textview的引力設置爲中心。

您可以根據自己的需要管理第二個佈局。

這是我的第一個XML佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <ListView 
     android:id="@+id/listView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" > 
    </ListView> 

</RelativeLayout> 

這裏是我的第二個佈局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/RelativeLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:gravity="center" 
     android:text="TextView" /> 

</RelativeLayout> 

如果您仍然面臨任何問題,那麼你可以問。如果您需要更多的靈活性,那麼您可以通過繼承ArrayAdapter類來創建自定義適配器。

這是自定義適配器的例子:How do I link a checkbox for every contact in populated listview?

希望它會幫助你。

+0

感謝您的解決方案。他們都是偉大的。我設法讓你的幫助工作,我只是一個問題。如果我需要一些靈活的列表視圖來包裝其寬度(不匹配),我還需要一個自定義適配器嗎?我只希望用戶能夠點擊文本,但不能點擊文本左右的空白區域。 – thhVictor

+0

我還沒有嘗試,但我認爲在這種情況下,你應該去自定義適配器,並在getView中,你應該爲你的textview添加onclicklistner。自定義適配器非常靈活,您可以根據自己的需求隨時更改它們。今天,如果您將按照您的要求實現您的列表視圖,並且稍後您需要更改或添加一些視圖,那麼您將再次執行所有操作。所以如果你覺得可以進一步改變,那麼你應該使用自定義適配器。 –

+0

好的謝謝你的提示。這就是我現在需要的。 – thhVictor