您好我在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"/>
答案 0 :(得分:2)
除非您的Fragment是静态内部类,否则您可以为ActionBar设置自定义标题。
要执行此操作,在使用自定义布局时,请在片段中调用getActivity().setTitle(YOUR_TITLE)
。
您致电setTitle
的原因是因为您正在调用getTitle
作为ActionBar的标题。这将返回该活动的标题。
如果您不想接听电话getTitle
,那么您需要创建一个公共方法,在托管片段的活动中设置TextView的文本。
在您的活动中,您需要输入以下代码:
public void setActionBarTitle(String title){
YOUR_CUSTOM_ACTION_BAR_TITLE.setText(title);
}
此外,请在您的片段中包含此代码:
((MainFragmentActivity) getActivity().getApplicationContext()).setActionBarTitle(YOUR_TITLE);
2015年更新:
似乎去年,Android有updated这样做的方式。如果您要为以后的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);
}
}
希望这会有所帮助:)
答案 1 :(得分:0)
请你试着把下面的行放到onCreate:
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setTitle("Title");
答案 2 :(得分:0)
尝试以下代码,希望有帮助
setActionBar(toolbar);
getActionBar().setDisplayShowTitleEnabled(true);
getActionBar().setTitle("Your Title");
答案 3 :(得分:0)
我认为这种逻辑可以帮助你
(第一种方式):
在您的片段调用中添加以下代码
getActivity().setTitle("your title");
(第二种方式): 调用父Activity或片段类的任何类型的函数
YourActivity mActiviy = (YourActivity) getActivity();
mActivity.yourActivityFunction(yourParameters);