我正在制作一个显示电话联系人的列表。它可以工作,但在横向方向上看起来不太好,因为它只是伸展开来。
所以我决定在 Landscape 中将其设为两列,在Portrait 中设为一列。我似乎无法通过谷歌搜索找到任何参考。
有什么办法吗?如果我只需要将自定义 XML 放在layout-land文件夹中,那就太好了
谢谢
[更新]
在肖像中它将是这样的:
name 1
name 2
name 3
name 4
在横向:
name 1 name 2
name 3 name 4
查看http://developer.android.com/training/basics/fragments/index.htmlFragment _
基本上你会有两个布局文件夹,就像你建议的那样。
您将把您的界面构建成Fragments,并将其中两个放在横向,一个放在纵向。例如
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>
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>
您应该使用过 GridView。根据方向将 numColumns 字段更改为 1 或 2。