[英]Setting tool bar title in fragment with DrawerLayout
嗨,我在DrawerLayout中有一個地圖片段視圖。 活動看起來像這樣。
我想向工具欄添加標題。 我嘗試了幾種選擇都沒有用。 有人可以幫我設置這項活動的標題嗎?
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
<?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>
活動用戶詳細信息
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);
}
表現
<activity android:name=".UserDetails"
android:label="User Details"/>
除非您的Fragment是靜態內部類,否則您可以為ActionBar設置自定義標題。
為此,在使用自定義布局時,在您的Fragment中,調用getActivity().setTitle(YOUR_TITLE)
。
調用setTitle
的原因是因為您將getTitle
稱為ActionBar的標題。 這將返回該活動的標題。
如果不想調用getTitle
,則需要創建一個公共方法,該方法在承載Fragment的Activity中設置TextView的文本。
在您的活動中,您需要輸入以下代碼:
public void setActionBarTitle(String title){
YOUR_CUSTOM_ACTION_BAR_TITLE.setText(title);
}
另外,在您的片段中包含以下代碼:
((MainFragmentActivity) getActivity().getApplicationContext()).setActionBarTitle(YOUR_TITLE);
2015年更新:
似乎去年,Android已更新了完成方式。 如果要為以后的API進行構建,則需要使用以下命令:
在您的片段中:
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);
}
}
在您的活動中:
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);
}
}
希望這可以幫助 :)
您能否嘗試將以下行放在onCreate上:
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setTitle("Title");
嘗試下面的代碼,希望對您有所幫助
setActionBar(toolbar);
getActionBar().setDisplayShowTitleEnabled(true);
getActionBar().setTitle("Your Title");
我認為這種邏輯對您有幫助
(第一種方式):
在片段調用中添加以下代碼
getActivity().setTitle("your title");
(第二種方法):調用父活動或片段類的任何類型的函數
YourActivity mActiviy = (YourActivity) getActivity();
mActivity.yourActivityFunction(yourParameters);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.