2017-10-11 73 views
0

我有一個包含該結構的BaseActivity:重新覈准佈局butterknife

<!-- activity_base --> 
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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:id="@+id/container_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true"> 

    <include 
     layout="@layout/tool_bar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentTop="true" /> 

    <include 
     layout="@layout/content" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/tool_bar" 
     android:layout_above="@id/bottom_navigation" /> 

    <android.support.design.widget.BottomNavigationView 
     android:id="@+id/bottom_navigation" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     app:menu="@menu/bottom_navigation_menu" /> 

</RelativeLayout> 

在我BaseActivity我控制BottomNavigationView。我創建了幾個活動,我想利用包含內容並只加載該活動的內容。 今天該項目正在使用butterknife Java。而且該項目越來越龐大,因爲它正在複製每個子活動的頂層結構BaseActivity。我想重構(以最好和最快的方式)該項目,以便只有一個 activity_base.xml和其他活動僅控制其內容(content.xml)。 我看到一些使用ViewStub和另一個描述<merge>但我不明白如何在項目中輕鬆應用概念,因爲許多使用活動和片段,我的項目只與活動。

+0

之後調用它的原因是否可以爲所有活動使用完全相同的layout.xml? –

回答

0

我不是這個解決方案完全滿意,但我結束了類似的東西在一個類似的案子:

base_activity.xml

<...> 
    ..... Whatever you want at the top 
<.../> 


<!-- Container where your child Activity layout will be inflated --> 
<FrameLayout 
    layout="@+id/child_activity_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 

<...> 
    ..... Whatever you want at the bottom 
<.../> 

BaseActivity

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_base); 

    ViewGroup parent = findViewById(R.id.child_activity_container); 
    View childView = inflateChildLayout(LayoutInflater.from(this), parent); 
    // The child Activity can have no layout (for some reason) 
    if (childView != null) { 
     parent.addView(childView); 
    } 

    ButterKnife.bind(BaseActivity.this); 

    // ... the rest of your onCreate 

} 

@Nullable 
protected abstract View inflateChildLayout(LayoutInflater inflater, ViewGroup parent); 

ChildActivity

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
//  setContentView(...); // Already managed by the base Activity so we don't need it 
} 

@Override 
protected View inflateChildLayout(LayoutInflater inflater, ViewGroup parent) { 
    return inflater.inflate(R.layout.activity_child, parent, false); 
} 

的想法是BaseActivity管理子佈局的通貨膨脹其自身的內部佈局和管理呼叫ButterKnife。由於父母和孩子實際上是相同的實例(就像ChildActivity擴展了BaseActivity一樣)ButterKnife父母和孩子的Views。這就是爲什麼在撥打inflateChildLayout

+0

我不知道ViewSub,所以你可以用它來改善上面的代碼。 – Eselfar