简体   繁体   中英

Single column ListView in Portrait but Two columns in Landscape?

I'm making a list that display Phone Contact. It works but does not looks good in Landscape orientation since it just stretched out.

So I decided to make it two column while in Landscape and one column in Portrait . I can't seem to find any reference by googling.

Is there any way to do it? It would be great if I just need to put custom XML in layout-land folder

Thanks

[UPDATE]

In Portrait it will be like this:

name 1
name 2
name 3
name 4

In Landscape:

name 1    name 2
name 3    name 4

Look into Fragment s http://developer.android.com/training/basics/fragments/index.html

Basically you will have two layout folders for this, just like you suggested.

  • res/layout/main.xml (use for portiat)
  • res/layout-land/main.xml (use for landscape)

You'll build your interface into Fragment s and place two of them in the landscape and one in portrait. For example

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.
    }
}

res\\layout\\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>

res\\layout-land\\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>

You should have used a GridView. Depending on the orientation change the numColumns field to 1 or 2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM