2012-11-20 40 views
1

我正在製作一個顯示Phone Contact的列表。它可以工作,但在風景方向上看起來不太好,因爲它只是延伸出來。縱向單列ListView但橫向兩列?

所以我決定在和縱向的一列中做出兩列。我似乎無法通過Google搜索找到任何參考。

有沒有辦法做到這一點?這將是巨大的,如果我只是需要把自定義XML在layout-land文件夾

感謝

[更新]

在人像這將是這樣的:

name 1 
name 2 
name 3 
name 4 

景觀:

name 1 name 2 
name 3 name 4 

回答

1

調查Fragment s http://developer.android.com/training/basics/fragments/index.html

基本上,您將擁有兩個佈局文件夾,就像您建議的一樣。

  • RES /佈局/ main.xml中(用於portiat)
  • RES /佈局,土地/ main.xml中(用於景觀)

你建立你的界面進入Fragment小號並將其中兩個放在風景中,另一個放在肖像中。例如

public class ContactListFragment extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View view = inflater.inflate(R.layout.contact_list, container, false); 
     // do your work 
     return view; 
    } 
} 

public class MainActivity extends FragmentActivity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // configure your fragments here. 
    } 
} 

資源\佈局\ main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <fragment android:name="com.example.android.fragments.ContactListFragment" 
       android:id="@+id/contact_list_fragment" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" /> 

</LinearLayout> 

資源\佈局,土地\ main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <fragment android:name="com.example.android.fragments.ContactListFragment" 
       android:id="@+id/contact_list_fragment1" 
       android:layout_weight="1" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" /> 

    <fragment android:name="com.example.android.fragments.ContactListFragment" 
       android:id="@+id/contact_list_fragment2" 
       android:layout_weight="1" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" /> 


</LinearLayout> 
+0

哇,我永遠不知道我們可以做到這一點,太棒了。但是我想要的是在景觀中並排的兩列聯繫人姓名。可能嗎?我更新了這個問題,所以不會造成混淆,對不起 – hrsetyono

+0

在這種情況下,您會希望使用相同的'ContactListFragment'兩次,並且只顯示您想要的內容。我將更新示例 – Kirk

+0

謝謝,我會先嚐試 – hrsetyono

0

你應該用一個GridView。取決於方向將numColumns字段更改爲1或2.