gpt4 book ai didi

android - BaseActivity 中的工具栏

转载 作者:搜寻专家 更新时间:2023-11-01 07:47:04 25 4
gpt4 key购买 nike

我有多个 Activity 和 fragment 。我想在 BaseActivity 中设置工具栏(所以只设置一次)。但需要从 fragment 访问工具栏(方法如 - 显示、隐藏、更改标题等)有什么建议吗?

我尝试了下面的解决方案,但是当我想隐藏 fragment 时,会显示 NPException

public abstract class BaseActivity extends AppCompatActivity {

Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResource());
configureToolbar();
}

protected abstract int getLayoutResource();

private void configureToolbar() {
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}

public void hideToolbar() {
toolbar.setVisibility(View.GONE);
}

我的 Activity

public class MyActivity extends BaseActivity() {
}

我在 fragment 中调用 hideToolbar 如下:

public class MyFragment extends Fragment() {

onCreate() {

((Myactivity)getActivity).hideToolbar();
}

最佳答案

我在我的项目中包含了类似你的东西。这是 sample 。可以引用一下。

BaseActivity.java类:

public abstract class BaseActivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResource());
}


protected abstract int getLayoutResource();
}

ToolBarActivity.java 类:

public abstract class ToolbarActivity extends BaseActivity {
protected Toolbar toolbar;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View contentView = findViewById(R.id.flToolbarContentContainer);
if (contentView instanceof ViewGroup) {
((ViewGroup) contentView)
.addView(LayoutInflater.from(this)
.inflate(getToolbarLayoutResource()
, (ViewGroup) contentView, false));
}
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}

@Override
protected int getLayoutResource() {
return R.layout.activity_toolbar;
}

protected abstract int getToolbarLayoutResource();

public void showToolbar() {
toolbar.setVisibility(View.VISIBLE);
}

public void hideToolbar() {
toolbar.setVisibility(View.GONE);
}
}

activity_toolbar.xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rlToolbarContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">


<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_toLeftOf="@+id/pbToolbarActivity"
android:background="@color/blue_panel_day_background"
android:theme="@style/ToolbarTheme" />

<FrameLayout
android:id="@+id/flToolbarContentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar" />
</RelativeLayout>

MainActivity.java 类:

public class MainActivity extends ToolbarActivity {

@Override
protected int getToolbarLayoutResource() {
return R.layout.activity_main;
}

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

homeFragment = HomeFragment.newInstance();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.llMainActivityContainer, homeFragment)
.commit();
}
}

activity_main.xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llMainActivityContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

</LinearLayout>

现在,在 Fragment 类中,应用:

 ((ToolbarActivity) getActivity()).showToolbar();
((ToolbarActivity) getActivity()).hideToolbar();

关于android - BaseActivity 中的工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41862109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com