gpt4 book ai didi

java - 使用 DrawerLayout 在 fragment 中设置工具栏标题

转载 作者:行者123 更新时间:2023-12-02 03:23:11 25 4
gpt4 key购买 nike

嗨,我在 DrawerLayout 中有一个 map fragment View 。 Activity 看起来像这样。

enter image description here

我想向工具栏添加标题。我尝试了几种选择都没有效果。有人可以帮我设置此 Activity 的标题吗?

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>

Activity 用户详细信息

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);

}

list

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

最佳答案

除非您的 Fragment 是静态内部类,否则您可以为 ActionBar 设置自定义标题。

为此,请在使用自定义布局时,在 Fragment 中调用 getActivity().setTitle(YOUR_TITLE)

您调用 setTitle 的原因是您调用 getTitle 作为 ActionBar 的标题。这将返回该 Activity 的标题。

如果您不想调用 getTitle,那么您需要创建一个公共(public)方法,用于在托管 Fragment 的 Activity 中设置 TextView 的文本。

在您的 Activity 中,您需要输入以下代码:

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

另外,请将此代码包含在您的 fragment 中:

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

2015 年更新:

好像去年Android就有updated这样做的方式。如果您正在为以后的 API 进行构建,则需要使用:

在您的 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);
}
}

在您的 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);
}
}

希望这有帮助:)

关于java - 使用 DrawerLayout 在 fragment 中设置工具栏标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39342281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com