简体   繁体   English

使用DrawerLayout在片段中设置工具栏标题

[英]Setting tool bar title in fragment with DrawerLayout

Hi i am having a map fragment view inside a DrawerLayout. 嗨,我在DrawerLayout中有一个地图片段视图。 The activity look like this. 活动看起来像这样。

在此处输入图片说明

I want to add a title to the toolbar. 我想向工具栏添加标题。 i tried several option nothing worked. 我尝试了几种选择都没有用。 can some one help me to set the title for this activity. 有人可以帮我设置这项活动的标题吗?

Activity_user_details Activity_user_details

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    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/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start"
    tools:context="zupportdesk.desk.zupport.chatsystem.UserDetails">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context="com.learn2crack.myapplication.MainActivity">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:id="@+id/user_details_app_bar"
            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" />

            <View
                android:layout_width="fill_parent"
                android:layout_height="1dp"
                android:background="@color/colorWhite"/>

            <include layout="@layout/content_user_details" />




        </android.support.design.widget.AppBarLayout>


        <fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <Button
                android:layout_width="match_parent"
                android:text="@string/start_chat"
                android:textAllCaps="false"
                android:background="@drawable/button_style"
                android:textColor="@color/colorWhite"
                android:textSize="20dp"
                android:textStyle="bold"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" />
        </RelativeLayout>

    </android.support.design.widget.CoordinatorLayout>




    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/menu_navigation"/>

</android.support.v4.widget.DrawerLayout>

content_user_details content_user_details

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

    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:id="@+id/imageView2"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="10dp"
        android:src="@drawable/icone_avatar2" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Name"
        android:id="@+id/ud_TV_name"
        android:layout_alignTop="@+id/imageView2"
        android:layout_toRightOf="@+id/imageView2"
        android:layout_toEndOf="@+id/imageView2"
        android:layout_marginTop="10dp"
        android:textSize="20dp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Visitor ID"
        android:id="@+id/ud_TV_visitor_id"
        android:layout_below="@+id/ud_TV_name"
        android:layout_alignLeft="@+id/ud_TV_name"
        android:layout_alignStart="@+id/ud_TV_name"
        android:layout_marginTop="5dp"
        android:textSize="12dp"
        android:textColor="@color/colorWhite" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Start Time"
        android:id="@+id/ud_TV_start_time"
        android:layout_below="@+id/ud_TV_visitor_id"
        android:layout_toRightOf="@+id/imageView2"
        android:layout_toEndOf="@+id/imageView2"
        android:layout_marginTop="5dp"
        android:textSize="12dp"
        android:textColor="@color/colorWhite" />

</RelativeLayout>

Activity User Details 活动用户详细信息

public class UserDetails extends FragmentActivity implements OnMapReadyCallback {

    private DrawerLayout drawerLayout;
    private Toolbar toolbar;
    private GoogleMap mMap;

    private String visitor_id;
    private String visitor_name;
    private String start_time;

    public String ProfileId;
    public String profileToken;
    public String CompanyID;
    public String DisplayName;

    TextView ud_name;
    TextView ud_visitor_id;
    TextView ud_start_time;

    private SelectVisitorService mService = new SelectVisitorService();
    private boolean mBound = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_details);

        SharedPreferences prefs = getSharedPreferences("zupportdesk", MODE_PRIVATE);
        ProfileId = prefs.getString("ProfileId", "Not defined");
        profileToken = prefs.getString("profileToken", "Not defined");
        CompanyID = prefs.getString("companyId", "Not defined");
        DisplayName = prefs.getString("DisplayName", "Not defined");

        toolbar = (Toolbar) findViewById(R.id.toolbar);
       //setSupportActionBar(toolbar);
        initNavigationDrawer();


        ud_name = (TextView) findViewById(R.id.ud_TV_name);
        ud_visitor_id = (TextView) findViewById(R.id.ud_TV_visitor_id);
        ud_start_time = (TextView) findViewById(R.id.ud_TV_start_time);

        Intent intent = getIntent();
        visitor_id = intent.getStringExtra("visitor_id");
        visitor_name = intent.getStringExtra("visitor_name");
        start_time = intent.getStringExtra("start_time");

        ud_name.setText(visitor_name);
        ud_visitor_id.setText(visitor_id);
        ud_start_time.setText(start_time);


        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

    }

Manifest 表现

 <activity android:name=".UserDetails"
            android:label="User Details"/>

Unless your Fragment is a static inner class, you can set a custom title to the ActionBar. 除非您的Fragment是静态内部类,否则您可以为ActionBar设置自定义标题。

To do this, while using a custom layout, in your Fragment, call getActivity().setTitle(YOUR_TITLE) . 为此,在使用自定义布局时,在您的Fragment中,调用getActivity().setTitle(YOUR_TITLE)

The reason you call setTitle is because you're calling getTitle as the title of your ActionBar. 调用setTitle的原因是因为您将getTitle称为ActionBar的标题。 This returns the title for that Activity. 这将返回该活动的标题。

If you don't want to get call getTitle , then you'll need to create a public method that sets the text of your TextView in the Activity that hosts the Fragment. 如果不想调用getTitle ,则需要创建一个公共方法,该方法在承载Fragment的Activity中设置TextView的文本。

In your activity you need to enter this code: 在您的活动中,您需要输入以下代码:

public void setActionBarTitle(String title){
    YOUR_CUSTOM_ACTION_BAR_TITLE.setText(title);
}

Also, include this code in your fragment: 另外,在您的片段中包含以下代码:

((MainFragmentActivity) getActivity().getApplicationContext()).setActionBarTitle(YOUR_TITLE);

2015 Update: 2015年更新:

It seems that last year, Android have updated the way this is done. 似乎去年,Android已更新了完成方式。 If you are building for later APIs, you need to use this: 如果要为以后的API进行构建,则需要使用以下命令:

In your fragment: 在您的片段中:

public class UpdateActionBarTitleFragment extends Fragment {
private OnFragmentInteractionListener mListener;

    public UpdateActionBarTitleFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (mListener != null) {
            mListener.onFragmentInteraction("Custom Title");
        }
        return inflater.inflate(R.layout.fragment_with_action_bar, container, false);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " needs to use OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragmentInteractionListener {
        public void onFragmentInteraction(String title);
    }
}

In your activity: 在您的活动中:

public class UpdateActionBarTitleActivity extends ActionBarActivity implements UpdateActionBarTitleFragment.OnFragmentInteractionListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_with_action_bar);
    }

    @Override
    public void onFragmentInteraction(String title) {
        getSupportActionBar().setTitle(title);
   }
}

Hope this helps :) 希望这可以帮助 :)

Could you please try to put below lines to onCreate: 您能否尝试将以下行放在onCreate上:

    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setTitle("Title");

Try following code, hope that helps 尝试下面的代码,希望对您有所帮助

setActionBar(toolbar);
getActionBar().setDisplayShowTitleEnabled(true);
getActionBar().setTitle("Your Title");

I think this logic help you 我认为这种逻辑对您有帮助

(First way): (第一种方式):

In your fragment call add following code 在片段调用中添加以下代码

getActivity().setTitle("your title");

(Second way): call any type of function of your parent Activity or fragment class (第二种方法):调用父活动或片段类的任何类型的函数

YourActivity mActiviy = (YourActivity) getActivity();
mActivity.yourActivityFunction(yourParameters);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM