簡體   English   中英

Android Layout-如何根據屏幕尺寸創建動態表格布局?

[英]Android Layout - How to create a dynamic tablelayout based on screen size?

我需要為其創建2個片段布局嗎? 第一個布局是(名字,性別,類別等。),第二個布局是(姓氏,DOB等)。

然后使用片段根據屏幕尺寸進行排列?

但是我認為不是很好嗎? 因為這樣我們就需要確定我需要在一個布局中放置多少行。

實現它的最佳方法是什么?

在平板電腦中查看: 在此處輸入圖片說明

在電話中查看: 在此處輸入圖片說明

使用單個片段或活動,然后針對不同的屏幕尺寸和方向使用不同的布局。 如果使用單獨的片段,則處理用戶交互的代碼將位於多個位置。 使用單個Fragment或Activity將使您的代碼更簡單。

以下是一些示例布局,可幫助您入門。 我正在使用所有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),並將它們放在資源文件夾中,以適應不同的屏幕尺寸和方向。 (例如,布局和布局用地)。

然后在片段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;
}

由於TextView在兩種布局中都具有相同的android:id,因此無論加載哪種布局,您的代碼都可以將它們視為相同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM