一、動態規划界面的大小
1.我們在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>
二、精准選擇界面
1.我們在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; } }
三、源碼:
1.項目地址
https://github.com/ruigege66/Android/tree/master/FragmentTest
2.CSDN:https://blog.csdn.net/weixin_44630050
3.博客園:https://www.cnblogs.com/ruigege0000/
4.歡迎關注微信公眾號:傅里葉變換,個人公眾號,僅用於學習交流,后台回復”禮包“,獲取大數據學習資料

