一、動態規劃界面的大小
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.歡迎關注微信公眾號:傅里葉變換,個人公眾號,僅用于學習交流,后臺回復”禮包“,獲取大資料學習資料

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/4150.html
標籤:Android
