0

我需要为它创建2个片段布局吗?第一布局是(名字,性别,类别等),第二布局是(姓氏,DOB等...)Android布局 - 如何根据屏幕尺寸创建动态桌面布局?

然后使用片段根据屏幕大小进行排列?

但我不认为是好的吗?因为那时我们需要决定我需要在一个布局中放置多少行。

实现它的最佳方法是什么?

查看片剂: ​​

查看在电话: enter image description here

回答

1

使用一个单一的片段或活动,然后使用不同的布局不同的屏幕尺寸&方向。如果使用单独的片段,处理用户交互的代码将位于多个位置。使用单个片段或活动将使您的代码更简单。

以下是一些可帮助您入门的布局示例。我使用所有的TextView,但你可以很容易地用Spinner替换性别TextView。

对于电话或更小的屏幕,你会使用这个布局:

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

    <TextView 
     android:id="@+id/first_name" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Chee"/> 

    <TextView 
     android:id="@+id/last_name" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Kin"/> 

    <TextView 
     android:id="@+id/gender" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Male"/> 

    <TextView 
     android:id="@+id/date_of_birth" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="dd/mm/yy"/> 

</LinearLayout> 

然后横向或更大的屏幕,这其中将会把姓氏和出生日期在第二列。

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <TextView 
      android:id="@+id/first_name" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Chee"/> 

     <TextView 
      android:id="@+id/last_name" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Kin"/> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <TextView 
      android:id="@+id/gender" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Male"/> 

     <TextView 
      android:id="@+id/date_of_birth" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:hint="dd/mm/yy"/> 

    </LinearLayout> 
</LinearLayout> 

给他们相同的文件名(如person_info.xml),并把他们在资源文件夹为不同的屏幕尺寸&方向。 (例如布局& layout-land)。

在你的片段onCreate()中,然后膨胀布局和Android处理根据用户屏幕大小&方向选择正确的文件。

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     fragmentView = inflater.inflate(R.layout.person_info, container, false); 

     firstNameTextView = (TextView) fragmentView.findViewById(R.id.first_name); 
     lastNameTextView = fragmentView.findViewById(R.id.last_name); 
     genderTextView = (TextView) fragmentView.findViewById(R.id.gender); 
     dobTextView = (TextView) fragmentView.findViewById(R.id.date_of_birth); 

     return fragmentView; 
} 

由于TextViews具有相同的机器人:ID在这两个布局,你的代码可以把他们相同的,无论其布局被加载。

+0

非常感谢。你的建议很好的解决了我的问题,因为起初我也认为如果使用单独的片段,那么代码的维护将会令人头疼。 – guaz 2014-09-01 08:11:53