为每个片段添加一个动作栏 [英] add an actionbar for each fragment
问题描述
假设我在mainActivity中放入了4个片段,我想在每个片段中创建不同的操作栏.
Suppose I have 4 fragments that I put in mainActivity, I want to create different actionbar different in each frgament.
当我按下片段1时,actionBar出现片段1,当我按下出现的两个片段时,actoinbar属于片段2
When I press fragment 1 then the actionBar appears fragment 1, when I press the two fragment that appears actoinbar belonging to fragment 2
01-02 20:12:33.700 15035-15035/src.go_letskerja E/AndroidRuntime: FATAL EXCEPTION: main
Process: src.go_letskerja, PID: 15035
java.lang.NullPointerException
at src.go_letskerja.Fragment.OtherFragment.onCreateView(OtherFragment.java:26)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5113)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
at dalvik.system.NativeStart.main(Native Method)
这是我的活动:
package src.go_letskerja.Fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import src.go_letskerja.R;
/**
* A simple {@link Fragment} subclass.
*/
public class OtherFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_other, container, false);
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("TES");
//((AppCompatActivity)getActivity()).getSupportActionBar().setCustomView(R.layout.actionbar);
return v;
}
}
这是我的XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="60dp"
android:fitsSystemWindows="true"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:text="Tinggi Badan"
android:textSize="14dp" />
<TextView
android:id="@+id/tinggibadan"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="15dp"
android:paddingLeft="30dp"
android:paddingTop="10dp"
android:text="000 Cm"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/list" />
</LinearLayout>
推荐答案
是的,您可以将自定义工具栏添加到片段中,并在清单中没有操作栏的情况下创建活动主题,并执行类似的操作.这种布局可能是您的片段布局,并且会在您的活动中膨胀.
Yes, you can add the custom toolbar to your fragment and make your activity theme with no action bar in the Manifest and do something like this. this layout may be your fragment layout and inflate in your activity.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:map="http://schemas.android.com/apk/res-auto"
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="?actionBarSize"
android:layout_alignParentTop="true"
android:background="@color/colorPrimary"
android:gravity="top"
app:titleTextColor="@color/white">
<ImageView
android:id="@+id/iv_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:contentDescription="TODO"
android:visibility="visible"
app:srcCompat="@drawable/ic_back" />
<TextView
android:id="@+id/toolbar_title"
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="List Entry"
android:textColor="@color/white"
android:textSize="@dimen/large_text_size" />
<ImageView
android:id="@+id/iv_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="@dimen/middium_padding"
android:contentDescription="TODO"
android:src="@mipmap/add"
android:visibility="visible" />
</android.support.v7.widget.Toolbar>
</RelativeLayout>
并使用片段视图访问您的工具栏.
and access you toolbar with framgment view.
这篇关于为每个片段添加一个动作栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!