2017-03-05 58 views
1

我创建了20页的应用程序(从页面到页面),阅读了一些信息后,我终于混淆了最佳解决方案。添加新的布局,片段或新的Acitvity?

  1. 只创建新布局并将它们添加到同一个MainActivity?

  2. 创建每次新的Activity +新布局?

  3. 创建碎片并将它们添加到同一个MainActivity?

碎片和布局之间的实际区别是什么?

Java中用于添加新布局/片段的代码是什么?

谢谢。

+0

这可能是你的兴趣无穷多个HTTP ://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/ –

+0

谢谢Majeed :) – Melisa

回答

1

使用Fragments创建一个活动主页,如下所示。

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.example.myproj.activity.MainActivity"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 
     <android.support.design.widget.AppBarLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:theme="@style/AppTheme.AppBarOverlay"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="wrap_content" 
       android:layout_height="?attr/actionBarSize" 
       android:background="?attr/colorPrimary" 
       app:popupTheme="@style/AppTheme.PopupOverlay"> 

      </android.support.v7.widget.Toolbar> 

     </android.support.design.widget.AppBarLayout> 


     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 
      <FrameLayout 
       android:id="@+id/container" 
       android:name="com.example.HomeFragment" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" /> 
     </RelativeLayout> 
    </LinearLayout> 
</android.support.design.widget.CoordinatorLayout> 

//设计乌尔片段

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/white" 
    android:clickable="true"> 
..... 
..... 
..... 
</LinearLayout> 

// JAVA

public class MainActivity extends AppCompatActivity{ 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      ... 
       // call MyFragment page here 
      } 
    ..... 
    ..... 
    ..... 
    } 

//片段

public class MyFragment extends Fragment{ 
     View view; 
     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstantState){ 
      view = inflater.inflate(R.layout.fragment_page,container,false); 
    ... 

     return view; 
     } 
} 
+0

非常感谢。这里是我的投票为您的帖子Shunmugapriya。 – Melisa

+0

很高兴听到..如果你得到答案,请点击打勾..通过你的投票,其他人也可以从中受益 –

0

布局只是XML资源定义如何布局应该屏幕上看起来很像。碎片完全不同。将碎片视为具有与之相关的布局的小型活动。就像活动一样,当创建片段时,他们膨胀一个布局,然后渲染该布局。然而碎片需要附加到一个活动中,没有它的情况下它们不能存在。

对你来说,最好的办法是到ViewPager中使用的片段,这使您可以刷卡由左到右(反之亦然),并具有网页

+0

谢谢Isuru。这将允许保存来自每个页面的数据(按下单选按钮)并将其存储在内存中直到第20页? – Melisa

+0

是的,包含所有片段的活动会将它们留在内存中,直到您明确要求它们分离。 – IsuruKusumal

+0

很酷。非常感谢:)现在让我们看看它是如何工作的。 – Melisa