2012-11-20 39 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.