用butterknife重新批准布局

时间:2017-10-11 13:59:31

标签: android layout butterknife

我有一个包含这种结构的BaseActivity:

<!-- 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>的内容,但我不明白如何在项目中轻松应用概念,因为许多人使用活动和片段,我的项目仅用于活动。

1 个答案:

答案 0 :(得分: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父视图和子视图。这就是为什么在调用inflateChildLayout

之后调用它
相关问题