Java 如何在工具栏中为每个片段设置标题和后退箭头按钮?
//这是我的片段Java 如何在工具栏中为每个片段设置标题和后退箭头按钮?,java,android,Java,Android,//这是我的片段 <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android"> <ScrollView
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<RelativeLayout
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="@dimen/_420sdp"
android:background="#EAEAEA"
>
<RelativeLayout
android:id="@+id/home_bg_layout"
android:layout_width="match_parent"
android:layout_height="@dimen/_400sdp"
android:background="@drawable/sama">
<Button
android:id="@+id/bookAppointmentBtn"
android:layout_width="match_parent"
android:layout_height="@dimen/_55sdp"
android:layout_marginLeft="@dimen/_30sdp"
android:layout_marginRight="@dimen/_30sdp"
android:fontFamily="@string/roboto_light"
android:textSize="@dimen/_20sdp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="@dimen/_10sdp"
android:background="@drawable/book_appointmentbg"
android:textColor="@color/white"
android:text="@string/bookappointment" />
</RelativeLayout>
</RelativeLayout>
<Button
android:id="@+id/ContactNoBtn"
android:layout_width="match_parent"
android:layout_height="@dimen/_46sdp"
android:text="@string/shop_number"
android:textColor="#ffffff"
android:textSize="@dimen/_25sdp"
android:layout_below="@+id/discountsTv"
android:textAllCaps="false"
android:layout_marginRight="@dimen/_30sdp"
android:layout_marginTop="@dimen/_5sdp"
android:layout_marginLeft="@dimen/_30sdp"
android:background="@drawable/black_background"/>
<LinearLayout
android:id="@+id/servicesLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:layout_marginLeft="@dimen/_20sdp"
android:layout_marginRight="@dimen/_20sdp"
android:orientation="horizontal"
android:layout_below="@+id/servicesupperview"
>
<LinearLayout
android:id="@+id/menLayout"
android:layout_width="@dimen/_108sdp"
android:layout_height="@dimen/_27sdp"
android:layout_margin="@dimen/_10sdp"
android:layout_weight="1"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@string/roboto_medium"
android:textColor="#535353"
android:layout_marginStart="@dimen/_17sdp"
android:layout_marginTop="@dimen/_5sdp"
android:textSize="@dimen/_12sdp"
android:text="@string/mens"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/womenLayout"
android:layout_width="@dimen/_108sdp"
android:layout_height="@dimen/_27sdp"
android:layout_margin="@dimen/_10sdp"
android:layout_weight="1"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@string/roboto_medium"
android:textSize="@dimen/_12sdp"
android:layout_marginStart="@dimen/_10sdp"
android:layout_marginTop="@dimen/_5sdp"
android:textColor="#535353"
android:text="@string/womens"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/childrenLayout"
android:layout_width="@dimen/_108sdp"
android:layout_height="@dimen/_27sdp"
android:layout_margin="@dimen/_10sdp"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/_12sdp"
android:fontFamily="@string/roboto_medium"
android:textColor="#535353"
android:layout_marginStart="@dimen/_20sdp"
android:layout_marginTop="@dimen/_5sdp"
android:text="@string/children_caps"
/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
我正在
MainActivity
中使用工具栏。这是我的片段,我想在工具栏中设置标题、图像和后退按钮。在这里,我使用底部导航来更改片段。每个片段我想设置不同的标题和图像。我必须将标题设置为工具栏的中心,将后退按钮设置为工具栏的右角。在主活动中,在OnCreate()方法中添加这些行
要使用Back arrow,可以在onCreate()之外编写此代码
在您的片段中,您可以更改工具栏的标题
getActivity().setTitle("My fragment Title");
您可以使用
ImageView
和TextView
箭头创建自定义工具栏。
include_tool_bar.xml
<com.mastercard.wallet.ui.widget.CustomTextView
android:id="@+id/toolbar_title"
style="@style/Toolbar.TitleText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|left"
android:textSize="@dimen/px_48"
app:fontName="InterStateLight"
customviews:fontName="InterStateLight" />
<ImageView
android:id="@+id/toolbar_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="@string/adaCitiPayLogoText"
android:src="@drawable/ab_logo"
android:visibility="gone" />
工具栏的标题可以从片段更改为在创建的活动中调用该标题
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
((MainActivity) getActivity()).getSupportActionBar().setTitle(getString(R.string.menu_about_us));
((MainActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((MainActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
}
在活动中,请确保添加如下工具栏
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
对于背压,只需添加一个侦听器并执行所需的操作
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_action_back));
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// pop up fragment or take another action
}
});
有可能,你能在这里发布你的java/kotlin代码吗?检查这个链接:检查@AbhinavGupta评论中的链接我知道如何设置标题。问题是我必须将文本对齐到工具栏的中心。“问题是我必须将文本对齐到工具栏的中心”,然后按照@ChetanJoshi回答。记住,他希望这是动态的谢谢。他可以在任何片段中更改标题。通过调用片段中的set title
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
((MainActivity) getActivity()).getSupportActionBar().setTitle(getString(R.string.menu_about_us));
((MainActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((MainActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
}
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
setSupportActionBar(findViewById(R.id.toolbar));
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_action_back));
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// pop up fragment or take another action
}
});