我们如何在android中的所有父活动和子活动中添加工具栏和浮动操作按钮
Hi aim非常新,适用于android,在我的应用程序中,我添加了“工具栏”,在我的主要活动中添加了浮动操作按钮。好的,很好,请参阅下面的代码 在这里,我的主要要求是如何在所有其他剩余活动中添加此工具栏和浮动操作按钮我不明白,请帮助我一些(当然,工具栏是在所有其他活动中添加的,如果我们在父活动中添加一次,那么它也应用于子活动也可以) 但是我们如何在所有其他儿童活动中添加这个浮动动作按钮请帮我一些 activity_main.xml 儿童活动:-我们如何在android中的所有父活动和子活动中添加工具栏和浮动操作按钮,android,android-toolbar,Android,Android Toolbar,Hi aim非常新,适用于android,在我的应用程序中,我添加了“工具栏”,在我的主要活动中添加了浮动操作按钮。好的,很好,请参阅下面的代码 在这里,我的主要要求是如何在所有其他剩余活动中添加此工具栏和浮动操作按钮我不明白,请帮助我一些(当然,工具栏是在所有其他活动中添加的,如果我们在父活动中添加一次,那么它也应用于子活动也可以) 但是我们如何在所有其他儿童活动中添加这个浮动动作按钮请帮我一些 activity_main.xml 儿童活动:- 如果您试图在所有视图中仅使用一个工具栏和fab,
如果您试图在所有视图中仅使用一个
工具栏
和fab
,我建议您使用片段
。在活动布局和框架布局中声明toolbar
和fab
将包含所有片段。您确定SecondActivity
应称为MainActivity
的子级吗?无论如何,您也必须在SecondActivity
中定义fab
。我们如何才能做到这一点?我对这项技术非常陌生。请用一些代码@Rohit Aryano解释一下。不,我想在每个视图中使用单独的工具栏和fab,但我们如何做到这一点?我不明白。在这种情况下,请提供一些代码,您必须在所有活动中定义工具栏
和fab
。或者,您可以尝试扩展一个包含工具栏
和fab
的BaseActivity。顺便提一下我仍然建议你使用片段
,因为你可以随时更新你的工具栏
和fab
以获得不同的片段。请提供一些代码,然后我可以使用你的建议,我两天前刚刚在这项技术中输入的建议。如果你不介意,我将发送我的示例项目文件。请在我的应用程序中执行此操作。请帮助添加您的content\u main
layout文件。
<?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.venkat.actionbarapp.MainActivity">
<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="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" >
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_main);
}