動態添加頁面、創建一個新聞app

一、動態規劃界面的大小
我們在res的文件夾裏面創建一個新的文件夾large_fragment用來,然後寫一個界面,activity_main.xml文件,用於存儲平板電腦等一些分辨率高的界面。也就是說小屏幕使用正常activity_main文件、大屏幕就使用large_fragment文件夾裏面的界面。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent" >

​

    <fragment

        android:id="@+id/left_fragment"

        android:name="com.example.fragmenttest.LeftFragment"

        android:layout_height="match_parent"

        android:layout_width="match_parent" />

​

​

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent" >

   

    <fragment

        android:id="@+id/left_fragment"

        android:name="com.example.fragmenttest.LeftFragment"

        android:layout_width="0dp"

        android:layout_height="match_parent"

        android:layout_weight="1" />

   

    <fragment

        android:id="@+id/right_fragment"

        android:name="com.example.fragmenttest.RightFragment"

        android:layout_width="0dp"

        android:layout_height="match_parent"

        android:layout_weight="3" />

   

</LinearLayout>

二、精準選擇界面
我們在res下面建一個文件夾layout-sw600dp,這個就意味着如果屏幕的寬度小於600dp的時候,則會默認加載這個文件夾下面的activity_main​界面。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent" >

   

    <fragment

        android:id="@+id/left_fragment"

        android:name="com.example.fragment.LeftFragment"

        android:layout_width="0dp"

        android:layout_height="match_parent"

        android:layout_weight="1" />

       

    <fragment

        android:id="@+id/right_fragment"

        android:name="com.example,fragmenttest.RightFragment"

        android:layout_width="0dp"

        android:layout_height="match_parent"

        android:layout_weight="3" />

   

</LinearLayout>

三、我們在編寫app的時候總不能需要維護兩套代碼,但是我們接下來將會探究如果能夠編寫一個同時兼容電腦和手機的app​。
我們建立一個新聞的app,下面先寫一個新聞類,具體的細節我們下次在寫。

package com.example.fragmentbestpractice;

​

public class News {

 

  private String title;

 

  private String content;

​

  public String getTitle() {

    return title;

  }

​

  public void setTitle(String title) {

    this.title = title;

  }

​

  public String getContent() {

    return content;

  }

​

  public void setContent(String content) {

    this.content = content;

  }

 

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章